JAVASCRIPT

How To Send Emails Using Node.js


Since its introduction node.js has been quickly gaining widespread adoption and it is increasingly being used in place of older server side languages like PHP or Classic ASP. It boasts a large community of developers and as a result quite a number of modules have been created to expand and enhance its core functionality.

In this blog post, we will be looking at one of such modules called Nodemailer. Nodemailer is a very popular module that enables developers send emails from node.js applications. It contains a rich set of functions that make it easy for developers to use popular services like gmail, hotmail, sendgrid, mandrill etc to send emails and it even has a plugin architecture by which it can be extended.

The Basics

Getting started with nodemailer is pretty straightforward, just grab the module by installing with npm.

This process will install nodemailer and all of its dependencies within your node.js project.

To begin sending emails, first require the module then initialize the transporter object and finally call the sendMail function like so:

And voila!!! you have a working email program. Simply replace the options above with your own data and you’re good to go. In this case nodemailer looks for the email handling server for the receiving address, establishes a connection and maintains it until the email has been sent and it does all this without any extra effort on the part of the developer, how cool is that?

Once your email has been sent you can free up resources by calling

Popular Services

Sometimes, you might want to send emails through your existing account with one of the well known email services, well nodemailer has this functionality built in and supports over 20 email platforms including hotmail, gmail, yahoo, sendgrid and mandrill. In order to use this functionality in your app require both the nodemailer and nodemailer-smtp-transport module.

Note: Don’t forget to npm install nodemailer-smtp-transport

With the code above nodemailer connects and authenticates with the gmail smtp server and your email is sent through that account. Go here to get the complete list of services that nodemailer supports.

What happens if an email service you use is not included? Well nodemailer has got you covered, instead of specifying a service you can go ahead to specify the host and port of its SMTP server like so:

Nodemailer handles the rest for you!

I think it’s safe to say that nodemailer is the most extensive and robust email sending node.js module out there. We’ve just scratched the surface in this tutorial, nodemailer still has much more functions like sending messages over TLS, authenticating with servers using XOAUTH and pooling of emails to conserve resources.

Now you know how to build a simple email sending client using Node.js. In later subsequent posts, we’ll be looking at how to build an email server that can receive emails without using an external mail receiving service. Till then, Enjoy.

The link to the full working code is here

Share your thoughts in the comments below