Nodemailer
Written By: Avinash Malhotra
Updated on
Node JS Tutorial rating
Average rating: 5.0, based on
159 reviews
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.
- Zero dependency.
- Heavily Secure, no RCE vulnerabilities
- Unicode support including emojis 👍.
- Windows support
- Support Both plain text and html template
- Secure email delivery using TLS/STARTTLS
- ES6 Support.
- 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);