Sending Email in JavaScript using Nodemailer
Install Nodemailer via NPM
To install Nodemailer just go to your app directory and run this command
$ cd your-app-directory
$ npm install --save nodemailer
Sending an Email
To send an email we can set the transporter first, and then set the message body.
See the code bellow, in my example I assumed we are use Gmail SMTP as mail server.
require nodemailer from 'nodemailer';
// create a transporter
let mailTransporter = nodemailer.createTransport('smtps://your-user@gmail.com:your-pass-here@smtp.gmail.com');
// set-up email data
let mailOptions = {
from: 'Foo Baz ', //sender address
to: 'Recipient Name ', //recipient address
subject: 'Your mail subject here',
text: 'Your mail text here',
html: 'If contain html code, put your html code here',
}
// sending the email
mailTransporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
}
console.log('Message sent! ' + info.response );
})