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

问题描述 投票:0回答:2

我有这个

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('[email protected]');
            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
进行操作,没有错误,看起来没问题,但电子邮件未送达。

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

javascript node.js email ecmascript-6 sendgrid
2个回答
0
投票

纯粹是轶事,但是以下解释似乎阻止了许多人成功地将电子邮件从 Express Server 发送到 SendGrid API。

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

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


0
投票

可能性不大,但对于遇到此问题的任何人,请检查 SendGrid 仪表板中的 API 密钥访问权限。我的受到限制,由于某种原因只允许从本地主机发送电子邮件。启用“完全访问”使其可以在我的生产站点上运行。

© www.soinside.com 2019 - 2024. All rights reserved.