Features of Nodemailer
- It supports various features like adding HTML in the mail, Unicode characters, sending attachments with the text, etc.
- It uses the simple mail transfer protocol (SMTP). Below is the step-by-step approach to be followed to integrate this module into our application.
Step 1: Module Installation
Write the command in the terminal to install nodemailer and then import at the top of your nodejs application.
npm install nodemailer
Now we are ready to import this into our application.
const nodemailer = require('nodemailer');
Step 2: Create Transporter Object
There exists a createTransport method in nodemailer which accepts an object with some configurations and finally returns a transporter object. This object will be required later to send emails.
const transporter = nodemailer.createTransport(transport[, defaults]);
Here we are using Gmail as a service just for sample purposes, Although nodemailer can be easily integrated with any other mail service. In Gmail, we can either make our account less secure or we can use the Oauth2 security authentication unless normally google will not allow sending any mail via node.js.
Less Secure Account
Visit this link to make your account less secure, after doing this we can create our working transporter object just with the username and password of your Gmail account.
// app.js
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: secure_configuration.EMAIL_USERNAME,
pass: secure_configuration.PASSWORD
}
});
How to Send Email using Node.js ?
Sending emails is a common task in many applications, whether it be for sending notifications, account verification, password resets, or other communication needs. Node.js, with its event-driven and non-blocking architecture, is well-suited for performing such tasks. In this article, we’ll explore how to send emails using Node.js. We will cover setting up a mail transport, composing the email, and sending it.