ADVERTISEMENT
ADVERTISEMENT

Home » Node.js

How to send emails using Nodemailer | Node.js

Sending emails using Nodemailer: Here, we are going to learn how to send emails using Nodemailer in Node.js?
Submitted by Godwill Tetah, on September 12, 2019

Nodemailer is an npm module used to send emails. It has made the process of sending emails very easy.

Just like any other npm module, it is installed via npm using the command line.

Note: You should have a basic understanding of Node.js, Express and HTML.

Nodemailer has so many features and options which makes it interesting sending mails.

It is very flexible with all the protocol required for sending mails and can equally be integrated with email services.

One can begin sending/testing via the command line before actually creating a message form.

This article with gets you started with Nodemailer module.

Essentials

The developers of Nodemailer module say the following is required in your code,

nodemailer 1

The transporter is the service that carries your mail. For example in our example, we will use Gmail.

Quickly install Nodemailer by opening a terminal and typing npm install nodemailer.

Now, let's write our very first code.

Open a text editor and type the following code: save the file as app.js

var nodemailer = require('nodemailer');  

//setup the transporter
var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: { //  your gmail credentials to use it's service
    user: '[email protected]',
    pass: 'yourpassword'
  }
});

var mailOptions = {
  from: '[email protected]',  //sending from,
  to: '[email protected]',   // sending to
  subject: 'testing nodemailer',  // subje ct of your mail
  text: 'We Thank God it works'  // body of mail
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email Sent ' );
  }
});

Finally, start your node app and if sending is successful, the phrase 'email sent' will be printed out on the console or terminal.

nodemailer 2

Check you're the email's inbox.

Using Gmail as your transporter, you can also enable the less secure app access setting.

nodemailer 3

Note:

Sending email requires internet connection.. hahaha. I got someone there!! So you thought this will work offline huh!

Do not fear about your password security. It's a tested and secured module used by many since 2010.

To send to multiple recipients, simply add a comma sign after the first email you were sent to and continue adding other email addresses.

to: '[email protected], [email protected]'

Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question.

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.