Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodemailer with Gmail service not working on heroku

I've got a basic email setup done for sending an email using Nodemailer with AngularJS and NodeJS and I've got the project deployed on heroku.

The emailing seems to be working just fine when I am running the app on heroku, but when I get it deployed to Heroku no emails are sent.

For authentication, I am using a Gmail address and I also have a bcc to another Gmail address. So from and bcc addresses are two different Gmail addresses. The from address is the same as the address used for authentication.

Could somebody help me with resolving this issue?

Edit: Adding code

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: '[email protected]',
        pass: 'foobar'
    }
});

router.post('/send',function(req,res){

    var mailOptions = {
        from: 'Foo Bar ✔ <[email protected]>',
        to: req.body.email,
        subject: "Hello " + req.body.email,
        text: 'Hello ' + req.body.email + '✔',
        html: "<p>Hello " + req.body.email + " </p>",
        bcc: "[email protected]"
    };
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            console.log(error);
        }else{
            console.log('Message sent: ' + info.response);
            res.send(200);
        }
    });        
});
like image 878
skip Avatar asked Sep 05 '14 20:09

skip


People also ask

Does Nodemailer work with Heroku?

As far as I have tested, Nodemailer works fine on Heroku. If Nodemailer does not return an error, then the MDA server accepted the e-mail for delivery. You can check the exact response from the MDA (assuming you are using the SMTP transport) from the response object.

Can I use Gmail with Nodemailer?

To get Gmail working with nodemailer, most times, all you have to do is configure your google account by allowing access to "less secure apps" from the security. With this enabled, you could use your Google email address and password for nodemailer and start sending emails right away.

Does heroku have SMTP server?

You can indeed use external SMTP providers to send email through Heroku (I do this on several of the apps I run).

Does Nodemailer use SMTP?

SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so its truly universal. Almost every email delivery provider supports SMTP based sending, even if they mainly push their API based sending.


2 Answers

I believe this is an issue with google account security.

  • Google blocked your sign-in to use the mailing features due to an unknown device (location).

A few step to verify this:

  • Start your server locally and sends the email.

  • Check your account alerts for unknown sign-in.

This can be temporally resolved by: https://accounts.google.com/DisplayUnlockCaptcha

A more permanent resolution would be to change your password to a stronger level:

upper case letter + lower case letter + special symbols + numbers

like image 95
Alex Yan Avatar answered Sep 21 '22 16:09

Alex Yan


Instead of using direct gmail credentials like this

auth: {
    user: '[email protected]',
    pass: 'foobar'
}

Use OAuth2

 auth: {
    type: 'OAuth2',
    user: '[email protected]',
    accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
}

Google blocks the heroku IPs (unsafe), if we are using direct credentials like you mentioned above. You can refer this Medium article here

like image 43
Ebin Xavier Avatar answered Sep 19 '22 16:09

Ebin Xavier