使用SendGrid时收到以下错误。即使我收到错误邮件仍然发送。
null {statusCode:202,body:'',headers:{server:'nginx', 日期:'周三,2017年7月5日18:50:52 GMT', 'content-type':'text / plain;字符集= UTF-8' , 'content-length':'0', 连接:'关闭', 'x-message-id':'EUkSULsjRku6bJTkTFsQ9w', 'x-frame-options':'DENY', 'access-control-allow-origin':'https://sendgrid.api-docs.io', 'access-control-allow-methods':'POST', 'access-control-allow-headers':'授权,内容类型,代表x-sg-elas-acl', 'access-control-max-age':'600', 'x-no-cors-reason':'https://sendgrid.com/docs/Classroom/Basics/API/cors.html'}} _http_outgoing.js:357 抛出新的错误('不能在发送后设置标题。');
使用以下代码:
var helper = require('sendgrid').mail,
sg = require('sendgrid')('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');
var from_email = new helper.Email('my.mail');
var to_email = new helper.Email('my.mai');
var subject = 'Subject';
var content = new helper.Content('text/plain', 'test');
var mail = new helper.Mail(from_email, subject, to_email, content);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request, function(err, response) {
console.log(err, response);
if (!err) {
res.send({
message: 'An email has been sent to the provided email with further instructions.'
});
} else {
return res.status(400).send({
message: 'Failure sending email'
});
}
});
我该如何解决这个问题?
答案 0 :(得分:0)
A 202是成功消息,而不是错误。它只是表示您的邮件是“已接受”
http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
您的代码不应检测到任何20x响应的任何错误。