DEV Community

Cover image for Nodemailer Module
Nirupamvas
Nirupamvas

Posted on

Nodemailer Module

Hay everyone! I am Nirupamvas and today we will know about Nodemailer module in Nodejs and lets see its syntax,and how you write the code and where you can use it. So lets get started.

What is Nodemailer

Nodemailer is a single module with zero dependencies for Node.js, designed for sending emails. Its main features include (but are not limited to):

  • Platform-independence
  • HTML content and embedded image attachments
  • Unicode support
  • Security, in particular, email delivery with TLS/STARTTLS and DKIM email authentication

How to use Nodemailer

Installation

The only thing required to start using Nodemailer is Node.js version 6.0 or above. And you need to install Nodemailer itself but it's really easy with npm or Yarn package manager.Type the following command in Node.js command prompt:

npm install nodemailer
Enter fullscreen mode Exit fullscreen mode

or

yarn add nodemailer
Enter fullscreen mode Exit fullscreen mode

once you have completed you can include the module in any application:

var nodemailer = require('nodemailer');
Enter fullscreen mode Exit fullscreen mode

Send an Email

Now you are ready to send the emails from your servers.

Use the username and password from your selected email provider to send an email. In this section I will showing you with Gmail account to send an email:

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});
Enter fullscreen mode Exit fullscreen mode

And that's it! Now your server is able to send emails.

Multiple Receivers

To send an email to more than one receiver, add them to the "to" property of the mailOptions object, separated by commas.

Example

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend1@gmail.com, myfriend2@gmail.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
}
Enter fullscreen mode Exit fullscreen mode

Send HTML

To send HTML formatted text in your email, use the "html" property instead of the "text" property:

Example

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  html: '<h1>Welcome</h1><p>That was easy!</p>'
}
Enter fullscreen mode Exit fullscreen mode

NOTE

You need to do the following changes in you Google account in order to receive the email
Turning on 'less secure apps' settings as mailbox user
1)Go to your Google Account.
2)On the left navigation panel, click Security.
3)On the bottom of the page, in the Less secure app access panel, click Turn on access.If you don't see this setting, your administrator might have turned off less secure app account access.
4)Click the Save button.

And that's guys! Hope you like it if any quaries put them in the comments section.

Top comments (0)