How to send emails in Nodejs using nodemailer [2022 Guide].

Anand
Mar 15, 2023

Sending emails from node.js app or an api can be easily done using a popular library called nodemailer. In this tutorial, we will learn how you can send emails from nodejs app using nodemailer and a popular transactional email provider such as Mailgun or sendgrid.
Options for sending email in Node.js using nodemailer.
As I mentioned above, nodemailer provides two options to deliver emails from node.js. One is using plain SMTP method, and the other one is using any email api service such as Amazon SES, mailgun, sengrid etc.
What is SMTP
SMTP is a universal protocol for delivering emails. Even if you use a third party email service, they will give you the SMTP credentials such as SMTP Host, Port, Email, Password etc that can be used with an SMTP client. While SMTP is easier to setup, it has some drawbacks over using an API to deliver emails. Email API providers add additional layers of security and performance optimization to enhance email delivery. It is also less prone to DDOS attacks and hacking. In this article, we will learn both methods to send emails in node.js using SMTP and API.
What is nodemailer transport
Nodemailer has this useful feature called transports, which is the "provider" that makes us able to send mail from node.js app. SMTP is a transport, Mailgun, or Sendgrid or similar services are another transport options.
Installing Nodemailer
Install Nodemailer using the following command:
npm install nodemailer
OR
yarn add nodemailer
Once completed, include it into your web application
const nodemailer = require("nodemailer");
Sending email in Node.js using SMTP transport
One of the most common methods to send emails from a node.js app is to use the SMTP transport available in nodemailer. Now let's create the SMTP transport option in nodemailer. Use the SMTP credentials from your email provider. Almost all providers will generate an SMTP credential for you which could be accessed from their dashboard.
let transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
auth: {
user: "username",
pass: "password",
},
});
Now let's send the emails using the above SMTP transport.
transporter.sendMail({
from: 'from_address@example.com',
to: 'to_address@example.com',
subject: 'Test Email Subject',
html: '<h1>Example HTML Message Body</h1>'
}).then((res) => {
console.log("Success")
}).catch((err) => {
console.log("Sending failed", err);
});
Sending email in Node.js using email API providers.
As we discussed, mailgun provides built in transport plugins to deliver emails using popular email services such as Mailgun so you can make use of additional features for example dynamic email templates offered by Sendgrid
Sending email in node.js using Mailgun
Add the mailgun transport plugin to your nodejs project.
const mg = require("nodemailer-mailgun-transport")
Create the mailgun transporter.
const mailgunAuth = {
auth: {
api_key: "key-12341234123412341234",
domain: "Your domain names listed at https://mailgun.com/app/domains"
}
}
const mailgunTransport = nodemailer.createTransport(mg(mailgunAuth))
Now, let's simply send the email
const mailOptions = {
from: "myemail@example.com",
to: "recipient@domain",
subject: "EMAIL SUBJECT LINE",
html: "<h1>Hey, this is an email sent using mailgun transport in nodemailer"
}
mailgunTransport.sendMail(mailOptions, function(error, response) {
if (error) {
console.log(error)
} else {
console.log("Successfully sent email.")
}
})
There are built-in and external transport plugins for nodemailer that allows you to send emails from node.js app using any email service provider. I'll list the popular ones I could find below.
Bottom Line
Nodemailer is the easiest way to send emails from node.js app. With lots of third party plugins available, it is one of the most powerful email delivery plugins available in the node ecosystem. If you like to deliver email notifications combined with other channels such as SMS, WhatsApp, Slack, In-App, Push notifications etc without having to integrate multiple APIs, you should checkout our notification infrastructure service - Engagespot Read more tutorials related to sending emails.
© 2022 Engagespot. All rights reserved.