简体   繁体   中英

sending email via nodemailer not working

I have a simple piece of code which is not working as expected. I am using nodemailer.

var nodemailer = require('nodemailer');

var smtpTransport = nodemailer.createTransport('SMTP', {
  service: 'Gmail',
  auth: {
    user: 'personal gmail address',
    pass: 'password'
  }
});

var mailOptions = {
    from: 'personal gmail address',
    to: 'personal gmail address',
    subject: 'Hello world!',
    text: 'Plaintext message example.'
};

smtpTransport.sendMail(mailOptions, function(err) {
  console.log('Message sent!');
});

I am getting the Message Sent in console. But no emails in inbox.

For gmail service I use this :

const smtpTransport = require('nodemailer-smtp-transport')
const nodemailer = require('nodemailer')

const transport = nodemailer.createTransport(smtpTransport({
  service: 'gmail',
  auth: {
    user: 'email',
    pass: 'pass'
  }
}))

But you need to allow less secure authentication on your gmail account or emails are not sent. I think the steps are :

Can you check for the error message.

...
smtpTransport.sendMail(mailOptions, function(err) {
  if(err)
  {
    console.log(err);
  }
  else
  {
    console.log('Message sent!');
  }
});

Because in the sample code you had, even if the nodemailer returns valid error, it will still print Message Sent

Some times message can be queued or say any type of error in the mailOptions, the message cant be delivered.

Found this SO: self signed certificate in certificate chain error in mail . I needed to add this and it worked for me.

let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
           user: 'myemail@gmail.com', 
           pass: 'password' 
          },
    tls: {
            rejectUnauthorized: false
         }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM