0
我有这个Mailer.js
文件SendGrid不发送电子邮件(Node.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);
}
});
};
而且,里面的主要文件,我用这个scructure使用surveyRoutes
require('./routes/surveyRoutes')(app);
所以,当我试图发送电子邮件,与axios.post
熊,没有错误,它似乎是okey,但电子邮件未交付。
如果您发现代码存在任何问题,请告诉我。
可能是发送网格有问题。从sendgrip发送电子邮件时,我也面临同样的问题。 – sohamdodia
@sohamdodia你是什么意思,“发送电网的问题”?请解释。 – Remzes
可能你已经超过了发送网格的限制,或者你发送的发送网格邮件太多,所有邮件都会发送到垃圾邮件。所以你不能发送邮件。 – sohamdodia