Nodemailer with Gmail service not working on heroku

耗尽温柔 提交于 2021-02-06 10:15:43

问题


I've got a basic email setup done for sending 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 authenticaion 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 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: 'foobar@gmail.com',
        pass: 'foobar'
    }
});

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

    var mailOptions = {
        from: 'Foo Bar ✔ <foobar@gmail.com>',
        to: req.body.email,
        subject: "Hello " + req.body.email,
        text: 'Hello ' + req.body.email + '✔',
        html: "<p>Hello " + req.body.email + " </p>",
        bcc: "fred@gmail.com"
    };
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            console.log(error);
        }else{
            console.log('Message sent: ' + info.response);
            res.send(200);
        }
    });        
});

回答1:


I believe this is issue with google account security. - Google blocked your sign in to use the mailing features due to 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

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

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




回答2:


Instead of using direct gmail credentials like this

auth: {
    user: 'foobar@gmail.com',
    pass: 'foobar'
}

Use OAuth2

 auth: {
    type: 'OAuth2',
    user: 'user@example.com',
    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




回答3:


5 years later, I still struggled this problem (all answers found on SO failed in my case). Here is what I did, for now everything works smoothly:

  • Install SendGrid addon to your Heroku app (Free plan gives you 12k messages a month)
  • Go to SendGrid panel
  • Go to Marketing -> Senders to add your sender bot, configure it like this:
  • From = any_address@heroku.com
  • Reply = your_gmail@gmail.com

  • Generate API KEY here

  • Configure NodeMailer like so:

      const nodemailer = require('nodemailer'),
          sgTransport = require('nodemailer-sendgrid-transport');
    
      const mailTransporter = nodemailer.createTransport(sgTransport({
          auth: {
              api_key: process.env.ADMIN_EMAIL_API_KEY // your api key here, better hide it in env vars
          }
      }))
    
  • To send an email now, you have to add your gmail in 'Reply To' field, like so:

      mailTransporter.sendMail({
          from: `"Admin" <any_address@heroku.com>`,
          to: 'receiver@hotmail.com',
          replyTo: 'your_gmail@gmail.com',
          subject: 'Something',
          html: `Boom !`
      });
    

I think that's all, in case I forgot something, please add a comment below




回答4:


Try updating nodemailer package (using "npm update nodemailer" command)

var transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        user: 'youremai@gmail.com', // Your email id
        pass: 'pwd123' // Your password
    },
    tls: {
        // do not fail on invalid certs
        rejectUnauthorized: false
    }
});
router.post('/send',function(req,res){

    var mailOptions = {
        from: 'Foo Bar ✔ <foobar@gmail.com>',
        to: req.body.email,
        subject: "Hello " + req.body.email,
        text: 'Hello ' + req.body.email + '✔',
        html: "<p>Hello " + req.body.email + " </p>",
        bcc: "fred@gmail.com"
    };
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            console.log(error);
        }else{
            console.log('Message sent: ' + info.response);
            res.send(200);
        }
    });        
});


来源:https://stackoverflow.com/questions/25693280/nodemailer-with-gmail-service-not-working-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!