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