【问题标题】:Using sendgrid how to know whether my email bounced or get succesfully delivered in my code使用 sendgrid 如何知道我的电子邮件是否被退回或在我的代码中成功发送
【发布时间】:2016-10-14 14:13:12
【问题描述】:

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

【问题讨论】:

    标签: node.js email sendgrid


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2011-07-19
      相关资源
      最近更新 更多