0

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.

jerry
  • 745
  • 6
  • 20
user2281858
  • 1,957
  • 10
  • 41
  • 82

3 Answers3

2

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 :

jaumard
  • 8,202
  • 3
  • 40
  • 63
1

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_Outsider
  • 1,875
  • 2
  • 24
  • 42
chaimm
  • 192
  • 1
  • 8
0

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.

jerry
  • 745
  • 6
  • 20