Prakash Kumar
Prakash Kumar

Reputation: 879

Using sendgrid how to know whether my email bounced or get succesfully delivered in my code

I am using Sendgrid npm module for sending emails to my customers with node.js, now i am facing a problem here, when i send an email to an email which does not exist then my email is getting bounced but in my response on my server code i am getting "success" but on my sendgrid's dashboard it is showing me email as bounced, now can anyone please tell me how can i know in my code that whether my code bounced or successfully sent.

My code is given below

var options = {
        auth: {
            api_user: 'abc',
            api_key: 'pass'
        }
    }

    var mailer = nodemailer.createTransport(sgTransport(options));

    var email = {
        to: email_id,
        from: '[email protected]',
        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();
        }
    });

Also one more question here when i am sending my email using unsubscribed group id then my email is always getting delivered in gmail's "promotions" section can anyone please tell me how can i show my email to user in gmail's "updates" section.

Upvotes: 1

Views: 1560

Answers (1)

bwest
bwest

Reputation: 9814

The SendGrid API is asynchronous. Your request is accepted and then goes through several stages of processing including delivery. The API requests would take a very long time to respond if they had to wait for delivery to be attempted.

You have two options. The best option is to use the event webhook to receive events in real-time. There is some 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);
});

Or you can poll your suppression lists via API, using e.g. the bounces endpoint.

There is no way to control which tab gmail uses to display your message, it's based on Google's analysis of the content of the messages and your sending habits.

Upvotes: 2

Related Questions