Introduction
Sending emails is a crucial feature for many applications, as it enables applications to communicate important information to users, such as transaction notifications, password reset emails, automated emails, and more.
Setting up your Node.js application to send emails for various purposes like password reset or notifications can be tedious and time-consuming. This is where Nodemailer comes in as a solution.
What is Nodemailer
Nodemailer is a popular Node.js library that simplifies sending emails in your application. It provides a simple, user-friendly API that removes the inner workings of sending emails.
Additionally, Nodemailer offers a variety of transport methods such as SMTP, sendmail and AWS SES, giving developers more flexibility in how they send emails from their application.
With the introduction of Nodemailer, let's move on to getting started with this package and learn how to set it up and use it in your Node.js application to send emails with the Gmail service.
Prerequisites
This guide assumes that you have a basic understanding of Node.js.
You must have Node.js and npm (the Node.js package manager) installed on your machine to follow along. If you don't have them already, you can install them by following this guide: How to Install Node.js and npm on Windows
You can check if you have Node.js and npm installed by running the following commands in your terminal:
node -v
npm -v
You will also need a Gmail account to use the gmail service. If you don't have one, you can create one at www.gmail.com.
To use the Gmail service, you must generate an App Password by Google. To create an App Password, follow this link.
💡 App Passwords are only valid for a limited time, so you will need to generate a new one after a few months.
Getting Started With Nodemailer
The first step to getting started with Nodemailer is to install it in your project. You can do this by entering the following command in your terminal:
npm install nodemailer
This command will install the latest version of Nodemailer in your project and utilize it in your Node.js scripts.
Once Nodemailer is installed, you can require it in your script by using the following code:
const nodemailer = require('nodemailer');
This line of code loads the Nodemailer module and makes it available in your script.
The Transporter Object in Nodemailer
The transporter
object in Nodemailer is an object that enables you to send email messages. This object is created using the nodemailer.createTransport()
function, which takes an object as an input with the configuration options for the transport method you want to use.
When you create a new transporter
object, you need to provide the options required by your chosen transport method.
For example:
To create a new transporter object using the Gmail service:
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'email@gmail.com',
pass: 'App Password'
}
});
A new transporter
object is created in the above code block by calling the nodemailer.createTransport()
function and passing an options object as an argument. The options object passed is used to configure the transport method that the transporter
object will use to send emails.
The service property is set to 'gmail'
, which simply tells Nodemailer to use the Gmail service to send emails.
The auth
property contains the authentication details for the Gmail account.
The user
and pass
properties nested in the auth
property are used to specify the email address and App Password created that will be used to send the email.
Once you have created the transporter
object, you can use it to send email messages by calling its sendMail()
method. This method takes two arguments: an object that contains the email message options and a callback function that is called when the email is sent or if an error occurs.
Here is an example of how to use the sendMail()
method to send an email:
const mailOptions = {
from: 'youremail@example.com',
to: 'recipientemail@example.com',
subject: 'Nodemailer',
text: 'Learn about Nodemailer!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
The transporter.sendMail()
function is called in this code block with two arguments: the mailOptions
object and a callback function.
The mailOptions
object contains the options for the email message that will be sent. It has four properties:
-
from
: The email address that the email will be sent from. -
to
: The email address that the email will be sent to. -
subject
: The title of the email. -
text
: The body of the email.
The callback function is called after the email has been sent or if an error occurs. The function takes two arguments: error
and info
. If there is an error in sending the email, the particular error message will be logged to the console.
It's important to mention the info
object passed as the second argument to the callback function. The info
object contains valuable information about the sent email, such as the message id, the response from the email server, and the accepted recipients.
If the mail was successfully sent to the recipients, you will get a message in your console that looks like this:
Email sent: 250 2.0.0 OK 1674960048 o2-20020a05600c510200b003db16770bc5sm18825wms.6 - gsmtp
Customizations for the mailOptions Object
You can customize the email message by adding attachments and cc/bcc recipients to the mailOptions
object.
Adding Attachments to the Mail
To add attachments to an email, you can use the attachments
property of the mailOptions
object. This property is an array of attachment objects, each containing the following properties:
-
filename
: the name of the attachment file -
path
: the path to the attachment file
Here is an example of how to add an attachment to an email:
const mailOptions = {
from: 'youremail@example.com',
to: 'recipientemail@example.com',
subject: 'Nodemailer',
text: 'Learn about Nodemailer!',
attachments: [
{ // text file as an attachment
filename: 'file',
path: "file.txt"
},
{ // binary buffer as an attachment
filename: 'image',
path: 'pexels-photo-265129.jpeg'
}
]
};
Adding CC/BCC Recipients to the Mail
To add cc and bcc recipients to an email, you can use the cc
and bcc
properties of the mailOptions
object, respectively. These properties can take a single email address or an array.
For example:
const mailOptions = {
from: 'youremail@example.com',
to: 'recipientemail@example.com',
subject: 'Nodemailer',
text: 'Learn about Nodemailer!',
attachments: [
{ // text file as an attachment
filename: 'file',
path: 'file.txt'
},
{ // binary buffer as an attachment
filename: 'image',
path: 'pexels-photo-265129.jpeg'
}
]
cc: 'ccrecipient@example.com',
bcc: ['bccrecipient1@example.com', 'bccrecipient2@example.com']
};
In this example, you added a cc recipient, ccrecipient@example.com
, and two bcc recipients, bccrecipient1@example.com
and bccrecipient2@example.com
.
Importance of Nodemailer
Nodemailer is an essential tool for any Node.js developer looking to add email functionality to their application. It saves a lot of time needed to add this functionality from scratch and is an easy-to-use library.
Top comments (0)