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.
1 |
$ npm install nodemailer |
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:
1 2 3 4 5 6 7 8 9 10 |
var nodemailer = require('nodemailer'); var transporter = nodemailer.createTransport(); transporter.sendMail({ from: 'sender@address', to: 'receiver@address', subject: 'hello', html: '<b>hello world!</b>' text: 'hello world!' }); |
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
1 |
transporter.close(); |
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.
1 2 3 4 5 6 7 8 9 10 |
var nodemailer = require('nodemailer'); var smtpTransport = require('nodemailer-smtp-transport'); var transporter = nodemailer.createTransport(smtpTransport({ service: 'Gmail', auth: { user: 'username', pass: 'password' } })); |
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:
1 2 3 4 5 6 7 8 9 10 11 |
var nodemailer = require('nodemailer'); var smtpTransport = require('nodemailer-smtp-transport'); var transporter = nodemailer.createTransport(smtpTransport({ host: 'localhost', port: 25, auth: { user: 'username', pass: 'password' } })); |
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
- How To Deploy a Mean Stack App To Microsoft Azure - June 24, 2015
- How To Send Emails Using Node.js - June 23, 2015