Node Mailer

Nodemailer is a module used to send Mails in Node JS application. nodemailer is zero dependency module. Nodemailer use SMTP by-default. But other transporters are also available to use, like Amazon AWS SES, sendmail and stream.

Install Nodemailer

Nodemailer is available on NPM. To install nodemailer, use Node JS 6.0 or above.

npm i nodemailer


Nodemailer Features

Nodemailer was build in 2010. That time there was no mail transporter available for Node JS. Even today, Nodemailer is default choice to send mail in Node JS Application. Here are some features of nodemailer.

  1. Zero dependency.
  2. Heavily Secure, no RCE vulnerabilities
  3. Unicode support including emojis 👍.
  4. Windows support
  5. Support Both plain text and html template
  6. Secure email delivery using TLS/STARTTLS
  7. ES6 Support.
  8. async await used

usage

In this example, we will see a example to send mail through nodemailer.

     
const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
    host: "smtp.forwardemail.net",
    port: 465,
    secure: true,
    auth: {
    // replace `user` and `pass` values 
    user: 'REPLACE-WITH-YOUR-ALIAS@YOURDOMAIN.COM',
    pass: 'REPLACE-WITH-YOUR-GENERATED-PASSWORD'
    }
});
    
async function main() {
      // send mail with defined transport object
    const info = await transporter.sendMail({
    from: '"Fred Foo 👻" ',        // sender address
    to: "bar@example.com, baz@example.com",         // list of receivers
    subject: "Hello There✔", // Subject line
    text: "Hello world?", // plain text body
    html: "<b>Hello world?</b>",        // html body
});
    
console.log("Message sent: %s", info.messageId);
      
// Message sent:   

}
    
main().catch(console.error);