我正在使用Sendgrid npm模块通过node.js向我的客户发送电子邮件,现在我在这里面临一个问题,当我向不存在的电子邮件发送电子邮件时,我的电子邮件被退回,但在我的服务器代码响应中,我得到“成功”,但在我的sendgrid的仪表板上,它显示我的电子邮件被退回,现在有人可以告诉我如何在我的代码中知道我的代码是否退回或成功发送。
我的代码如下
var options = {
auth: {
api_user: 'abc',
api_key: 'pass'
}
}
var mailer = nodemailer.createTransport(sgTransport(options));
var email = {
to: email_id,
from: 'noreply@abc.com',
subject: 'ABC Verification Code',
text: "Your ABC Verification Code is " + totp
// html: '<b>Awesome sauce</b>'
};
mailer.sendMail(email, function(err, res) {
if (err) {
console.log(err);
res.json({success : 0, message : "Sorry Please try Again"});
return next();
} else {
console.log('res: ', res);
res.json({success : 1, message : "Successfully Sent Otp To Email Id"});
return next();
}
});
还有一个问题,当我使用未订阅的组 ID 发送电子邮件时,我的电子邮件总是在 gmail 的“促销”部分中发送,任何人都可以告诉我如何在 gmail 的“更新”部分中向用户显示我的电子邮件。
最佳答案
SendGrid API 是异步的。您的请求被接受,然后经过几个处理阶段,包括交付。如果 API 请求必须等待尝试交付,则需要很长时间才能响应。
你有两个选择。最好的选择是使用 event webhook实时接收事件。有一些nodejs event webhook sample code :
var express = require('express');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(express.bodyParser());
});
app.post('/event', function (req, res) {
var events = req.body;
events.forEach(function (event) {
// Here, you now have each event and can process them how you like
processEvent(event);
});
});
var server = app.listen(app.get('port'), function() {
console.log('Listening on port %d', server.address().port);
});
或者您可以通过 API 轮询您的黑名单,例如使用bounces端点。
无法控制 Gmail 使用哪个标签来显示您的消息,它基于 Google 对消息内容和您的发送习惯的分析。
关于node.js - 使用 sendgrid 如何知道我的电子邮件是否被退回或在我的代码中成功发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40045336/