0

i tried nodemailer but its not working. i want to send email to user through my node js website. can anyone please help me to get it done //code

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});
rotimi-best
  • 1,852
  • 18
  • 29
John
  • 55
  • 1
  • 10

2 Answers2

1

In my experience sending emails can be made a lot easier by outsourcing the work to a provider like https://www.mailgun.com, the cost is $0.0008 per email. The reason for this is not only simplicity but deliverability. So many people have sent so many fake and junk emails over the years, emails often end up in spam folders when the server is not set up correctly. Things like adding SPF records to domains can obviously help but in all honesty, I now just use a sending provider.

Fasani
  • 5,141
  • 1
  • 23
  • 24
  • 1
    This is an important point, especially if your service needs to send e-mails to others than yourself, and deliverability is important. – mhovd Apr 29 '20 at 11:42
  • No, I do not think so, I used it a few years ago last. They give you an API key so you would need that key connected to an account. It was also free up to 10k emails but that may have changed. – Fasani Apr 29 '20 at 15:18
0

Try this. The port is important to be 465.

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  },
  port: 465,
  secure: true,
  tls: {
    rejectUnauthorized: false
  }
});
rotimi-best
  • 1,852
  • 18
  • 29