使用sendgrid如何知道我的电子邮件是否在我的代码中被退回或成功交付

时间:2016-10-14 14:13:12

标签: node.js email sendgrid

我正在使用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的“更新”部分向用户显示我的电子邮件

1 个答案:

答案 0 :(得分:1)

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对消息内容和您的发送习惯的分析。