Have you ever wondered how emails are sent from within an application? Are you looking for a simple solution for sending emails from your Node.js backend? In this article, I will be showing you how you can use JavaScript to send emails.
To send emails with JavaScript, one ought to use a Node.js module called Nodemailer. This is a module that allows you to send emails easily. The official documentation describes it as "Send emails from Node.js β easy as cake! π°βοΈ". The latest version is 6.9.16 as of the date of this article and it seems to be regularly updated. Nodemailer supports all Node.js versions starting from Node.js@v6.0.0. You can install Nodemailer by running this command in your terminal:
npm install nodemailer
Node.js needs to be installed on the computer for commands that start with npm to work. When the installation is successful, you can access the Nodemailer module within a JavaScript file as shown below:
const nodemailer = require("nodemailer");
console.log(nodemailer);
This will display the following methods that are available from Nodemailer:
- createTransport
- createTestAccount
- getTestMessageUrl
Based on the function signatures alone, we can see that the createTestAccount function accepts arguments of apiUrl which is a string, and a callback function. This function then returns void. The getTestMessageUrl function accepts an argument called info which has a type of 'SESTransport.SentMessageInfo' or 'SMTPTransport.SentMessageInfo'. These are both string values.
The most used method from Nodemailer is createTransport. This simply configures the email account that will act as the sender of the emails. Here is how to set it up:
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: 'human_being@gmail.com',
pass: 'users-unknown-password',
},
});
The service property defines the email service provider of the email address. In this case it is Gmail. The auth object contains a user property which specifies an email address and a pass property which specifies the password. Even though Gmail is the fastest way to get started with sending emails, it is by no means a preferable solution unless you are using OAuth2 authentication. Gmail has measures to prevent bot accounts and requires the email address to be from a real person. This requires that the user sets up 2-factor authentication for their Gmail account and then create an βApplication Specificβ password for Nodemailer to work.
After defining the transporter variable above, we can proceed to create the email recipients, subject, and body. This is how to do it:
const mailOptions = {
from: 'human_being@gmail.com',
to: 'receiver@email.com',
subject: "The Subject of the Year!",
text: "The quick big brown fox jumped over the lazy dog."
html: `
<h1>Email Heading</h1>
</br>
<p>The quick big brown fox jumped over the lazy dog.</p>`,
};
- The from property specifies the email address that will send the email, the to property specifies the recipient, subject specifies the email subject, text specifies the plain text version of the email which can be used alone, html allows you to write HTML code and even apply inline styles. The text property is used as a default fallback option for email clients that do not support HTML.
We then proceed to call a method named sendMail which executes the email-sending process.
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.status(500).send({
message: `${error}`,
});
} else {
console.log("Email sent: " + info.response);
}
});
If configured correctly, the recipient email inbox should have the new message and the console should display something like this 'Email sent: 250 2.0.0 OK 1733756075 ffacd0b85a97d-3861ecf3cd1sm13191858f8f.11 - gsmtp'. If there is an error, the code above will output the relevant error message in the terminal. And just like that, we've seen how to send emails with JavaScript.
Further Analysis
We can learn more about the properties provided by the createTransport method by simply logging it to the terminal:
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: 'tawandamsengezi@gmail.com',
pass: 'rhbg litp nlkb zbgr',
},
});
console.log(transporter);
This will output all of the available methods, properties, and even the configuration options you previously defined for service and auth. Below are some examples and explanations of some methods that are provided by the createTransport object.
| Method | Description | Example |
|---|---|---|
close |
Closes all connections in the pool. If there is a message being sent, the connection is closed later | transporter.close() |
isIdle |
Returns true if there are free slots in the queue | transporter.isIdle() |
verify |
Verifies SMTP configuration | transporter.verify() |
Go ahead and celebrate!ππ₯³π

You just learnt how to send emails using JavaScript. Isn't that awesome!? Feel free to learn more about nodemailer by visiting the official website. Learning how to work with this tool can be a great addition to an existing application or creating one that sends emails. Make sure to give me a shout to share some feedback or ask a question. Until then, happy codingβοΈ!
