Sending an email using NodeMailer & Gmail

Manoj Singh Negi
codeburst
Published in
3 min readAug 1, 2017

--

I have just launched ProgrammerJournal.com. A standalone blog where I write about Javascript, Web development and software development.

Sending an email in nodejs is a breeze thanks to NodeMailer. Let me walk you through the process of sending an email using NodeMailer.

firstly install nodemailer in your node application.

npm install --save nodemailer

after installing the nodemailer, in your main node.js file require the nodemailer

var nodemailer = require('nodemailer');

nodemailer needs a transport service using which it can send emails. In this example I am using gmail.

inside your node.js file write

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

We used gmail as our transport service.

The next up is our auth object in which we have to specify our email address and password of gmail for allowing nodemailer to login and send email using our gmail account.

Now we need a second configuration object where we will be configuring our email details.

const mailOptions = {
from: 'sender@email.com', // sender address
to: 'to@email.com', // list of receivers
subject: 'Subject of your email', // Subject line
html: '<p>Your html here</p>'// plain text body
};

Most important four options are :-

from : The sender of the email

to : The recipient of the email

subject : the subject of the email

html : all the magic happens here

Before sending your email using gmail you have to allow non secure apps to access gmail you can do this by going to your gmail settings here.

Once less secure apps is enabled now nodemailer can use your gmail for sending the emails.

Now the last bit is actually sending the email we can do that by using sendMail method provided by the transporter object we created above.

transporter.sendMail(mailOptions, function (err, info) {
if(err)
console.log(err)
else
console.log(info);
});

sendMail takes two argument mailOptions and a callback function which will be called when the mail is sent. The callback function will be called when either email sent successfully our an error occurred.

And that’s how we send emails in nodejs using nodemailer and gmail.

Hi, My name is Manoj Singh Negi. I am a Javascript Developer and writer follow me at Twitter or Github.

I am available to give a public talk or for a meetup hit me up at justanothermanoj@gmail.com if you want to meet me.

Really loved this article ?

Please subscribe to my blog. You will receive articles like this one directly in your Inbox frequently.

Here are more articles for you.

  1. What are HTML Custom Elements
  2. Trying out Shadow Dom
  3. Building a React component as an NPM module
  4. Throttle function in javascript with the example
  5. React Context API Explained
  6. Introduction to Haskell Ranges
  7. HOC ( Higher-order components ) in ReactJS explained

Peace.

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.

--

--

I write about Javascript http://eepurl.com/co0DYj. Solving people problems using code. Javascript developer, Writer.