SendGrid不发送电子邮件(Node.js)

时间:2017-09-13 14:08:32

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

我有这个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一起发送,没有错误,而且似乎很好,但电子邮件未送达。

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

1 个答案:

答案 0 :(得分:0)

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

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

另外,我假设您的SendGrid API密钥已准确复制到Express Server上的密钥存储区。