【问题标题】:SendGrid does not send an email (Node.js)SendGrid 不发送电子邮件 (Node.js)
【发布时间】:2017-09-13 14:08:31
【问题描述】:

我有这个Mailer.js 文件

   const sendgrid = require('sendgrid');
    const helper = sendgrid.mail;
    const keys = require('../config/keys');

    class Mailer extends helper.Mail {
        constructor({ subject, recipients }, content) {
            super();

            this.sgApi = sendgrid(keys.sendGridKey);
            this.from_email = new helper.Email('no-reply@emaily.com');
            this.subject = subject;
            this.body = new helper.Content('text/html', content);
            this.recipients = this.formatAddresses(recipients);

            this.addContent(this.body);
            this.addClickTracking();
            this.addRecipients();
        }

        formatAddresses(recipients) {
            return recipients.map(({ email }) => {
                return new helper.Email(email);
            });
        }

        addClickTracking() {
            const trackingSettings = new helper.TrackingSettings();
            const clickTracking = new helper.ClickTracking(true, true);

            trackingSettings.setClickTracking(clickTracking);
            th

is.addTrackingSettings(trackingSettings);
    }

    addRecipients() {
        const personalize = new helper.Personalization();

        this.recipients.forEach(recipient => {
            personalize.addTo(recipient);
        });
        this.addPersonalization(personalize);
    }

    async send() {
        const request = this.sgApi.emptyRequest({
            method: 'POST',
            path: '/v3/mail/send',
            body: this.toJSON()
        });

        const response = await this.sgApi.API(request);
        return response;
    }
}

module.exports = Mailer;

还有,我有surveyRoutes.js文件,里面有关于路由的容器信息

const mongoose = require('mongoose');
const requireLogin = require('../middlewares/requireLogin');
const requireCredits = require('../middlewares/requireCredits');
const Mailer = require('../services/Mailer');
const surveyTemplate = require('../services/emailTemplates/surveyTemplate');

const Survey = mongoose.model('surveys');

    module.exports = app => {
        app.post('/api/surveys', requireLogin, requireCredits, async (req, res) => {
            const {title, subject, body, recipients} = req.body;

            const survey = new Survey({
                title,
                subject,
                body,
                recipients: recipients.split(',').map(email => ({ email: email.trim() })),
                _user: req.user.id,
                dateSent: Date.now()
            });

            const mailer = new Mailer(survey, surveyTemplate(survey));
            try {
               await mailer.send();
            }
            catch(e){
                console.log(e);
            }
            });
    };

而且,在主文件中,我使用这个结构来使用surveyRoutes

require('./routes/surveyRoutes')(app);

所以,当我尝试发送电子邮件时,使用axios.post 对其进行操作,没有错误,而且看起来还不错,但电子邮件未送达。

如果您发现代码有任何问题,请告诉我。

【问题讨论】:

  • 可能是发送网格有问题。从 sendgrip 发送电子邮件时,我也遇到了同样的问题。
  • @sohamdodia “发送网格有问题”是什么意思?请解释一下。
  • 可能是您超出了发送网格的限制,或者您从发送网格发送了太多邮件,所有邮件都进入了垃圾邮件。所以你不能发送邮件。

标签: javascript node.js email ecmascript-6 sendgrid


【解决方案1】:

纯属轶事,但以下解释似乎阻止了许多人从他们的 Express 服务器成功地将电子邮件发送到 SendGrid API。

许多人在注册了 SendGrid 服务的免费层后报告说,由于他们没有在他们的帐户设置中输入可验证的公司名称和公司网站地址,SendGrid 不会批准免费层访问,这体现在他们的应用程序无法通过 SendGrid 处理电子邮件。一个人实际上声称已经直接通过 SendGrid 支持验证了这种情况。

顺便说一句,我假设您的 SendGrid API 密钥已准确复制到您的 Express 服务器上的密钥存储区。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    相关资源
    最近更新 更多