Ulzurrun de Asanza i Sàez

Sending emails with NodeJS

Recently I’ve started a small project powered by NodeJS and it needs a way to send transactional emails (emails that are sent because an event is triggered). In my case the event is “clicking «I forgot my password» link”. Moreover, I want my project be easily deployed on a PaaS like Heroku so I prefer not relying on utilities like sendmail, or at least making it easy to move from sendmail to a third-party service like Postmark or Sendgrid.

With this two things in mind I looked for the best way to send mails in NodeJS and I  found something that maybe is not the best but it’s really easy to set up and use: an open source module named Nodemailer.

Using it is as easy as installing it via npm, creating a transport and calling a method.

Installing

Just run:

[code=bash]
npm install nodemailer
[/code]

I strongly recommend adding it to the dependencies in your package.js, but it is not required.

Creating a transport and sending an email

A transport is just a method to send emails. You can create a transport to send emails via Postmark (for those transactional emails, for instance) and a different transport to send emails via Sendgrid (to send newsletters). The transport is in charge to actually send the email.

If we want to send an email via sendmail we just create the appropriate transport:

[code=js]
// Require nodemailer.
var nodemailer = require( ‘nodemailer’ );

// Message we’ll send.
var sendmail_email = {
from: “SENDER-EMAIL-ADDRESS”,
to: “RECEIVER-EMAIL_ADDRESS”,
subject: “Hello world!”,
text: “Sendmail is working.”
};

// Sendmail transport.
var transport_sendmail = nodemailer.createTransport( “sendmail” );

// Send email.
transport_sendmail.sendMail( sendmail_email, function( error, message ) {
if ( error ) console.log( error );
else console.log( ‘Message sent’ );
} );
[/code]

If you try this your email will probably get marked as spam. That’s one of the reasons to rely on a third-party service. Let’s create a transport to send the emails with Postmark.

Moving from sendmail to Postmark

  1. Sign up for a Postmark account. It’s pretty easy but to sign the emails you’ll need a non-public email address (and to verify it you’ll also need access to its DNS records). I’ve used an email address @sumolari.com to test this. Setting up Postmark is really easy – it’s just following the steps that will be shown after signing up.
  2. Note the API key provided to you by Postmark. This API key will be used as username and password when authenticating to Postmark.
  3. Create a transport:

[code=js]
// Require nodemailer.
var nodemailer = require( ‘nodemailer’ );

// Message we’ll send.
var postmark_email = {
from: “SENDER-EMAIL-ADDRESS”,
to: “RECEIVER-EMAIL_ADDRESS”,
subject: “Hello world!”,
text: “Postmark is working.”
};

// Postmark transport.
var transport_postmark = nodemailer.createTransport( “postmark”, {
auth: {
user: “YOUR-API-KEY-HERE”,
pass: “YOUR-API-KEY-HERE”
}
});

// Send email.
transport_postmark.sendMail( postmark_email, function( error, message ) {
if ( error ) console.log( error );
else console.log( ‘Message sent’ );
} );
[/code]

As you can see just by changing the transport we can move from service to service which is really convenient (no one wants to be forced to use a service just because it was the one used at the beginning of project’s life cycle).


One reply on “Sending emails with NodeJS

  1. This post helped me setting up my Ghost blog to use Postmark. Thank you :)

Leave a Reply to Peter Szel Cancel reply

Your email address will not be published.

Required fields are marked *

Your avatar