Learn how to send an email using the nodemailer module in Node.js

How to send an email (gmail, outlook and Zoho) using nodemailer in Node.js

In this article, you'll learn how to send emails using the nodemailer module. Between the most know features of nodemailer are:

  • Node.js 0.10+, no ES6 shenanigans used that would break your production app.
  • Unicode to use any characters, including full emoji support.
  • Windows – you can install it with npm on Windows just like any other module, there are no compiled dependencies. Use it from Azure or from your Windows box hassle free.
  • HTML content as well as plain text alternative.
  • Attachments (including attachment streaming for sending larger files).
  • Embedded images in HTML.
  • Secure e-mail delivery using SSL/STARTTLS.
  • Different transport methods, either using built-in SMTP transports or from external plugins.
  • Custom plugin support for manipulating messages (add DKIM signatures, use markdown content instead of HTML etc.).
  • Sane XOAUTH2 login with automatic access token generation (and feedback about the updated tokens).
  • Simple built-in templating using node-email-templates or custom renderer.
  • Proxies for SMTP connections (SOCKS, HTTP and custom connections).

Requirements

In order to send emails in Node.js, as mentioned previously, you'll need the nodemailer module. To add nodemailer as a dependency in your project execute the following command in the Node.js command prompt:

npm install nodemailer

You can visit the official Github repository of nodemailer for more information or the package site in NPM here. After the download, you'll be able to require the module using "require('nodemailer')".

Send with a Gmail account

Google uses SSL encryption and the port 465.

Note: to use Gmail you may need to configure "Allow Less Secure Apps" in your Gmail account unless you are using 2FA in which case you would have to create an Application Specific password. You also may need to unlock your account with "Allow access to your Google account" to use SMTP.

var nodemailer = require('nodemailer');

// Create the transporter with the required configuration for Gmail
// change the user and pass !
var transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true, // use SSL
    auth: {
        user: '[email protected]',
        pass: 'myPassword'
    }
});


// setup e-mail data
var mailOptions = {
    from: '"Our Code World " <[email protected]>', // sender address (who sends)
    to: '[email protected], [email protected]', // list of receivers (who receives)
    subject: 'Hello', // Subject line
    text: 'Hello world ', // plaintext body
    html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }

    console.log('Message sent: ' + info.response);
});

Send with a Zoho account

Zoho mail uses SSL encryption and the port 465, the same as Gmail.

var nodemailer = require('nodemailer');

// Create the transporter with the required configuration for Gmail
// change the user and pass !
var transporter = nodemailer.createTransport({
    host: 'smtp.zoho.com',
    port: 465,
    secure: true, // use SSL
    auth: {
        user: '[email protected]',
        pass: 'myPassword'
    }
});

// setup e-mail data, even with unicode symbols
var mailOptions = {
    from: '"Our Code World " <[email protected]>', // sender address (who sends)
    to: '[email protected], [email protected]', // list of receivers (who receives)
    subject: 'Hello ', // Subject line
    text: 'Hello world ', // plaintext body
    html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }

    console.log('Message sent: ' + info.response);
});

Send with an Outlook (or hotmail) account

Outlook uses TLS encryption in the port 587 unlike Gmail or Zoho. In this case we need to disable the default secure connection to enable TLS using the SSLv3 encryption.

var nodemailer = require('nodemailer');

// Create the transporter with the required configuration for Outlook
// change the user and pass !
var transporter = nodemailer.createTransport({
    host: "smtp-mail.outlook.com", // hostname
    secureConnection: false, // TLS requires secureConnection to be false
    port: 587, // port for secure SMTP
    tls: {
       ciphers:'SSLv3'
    },
    auth: {
        user: '[email protected]',
        pass: 'myPassword'
    }
});

// setup e-mail data, even with unicode symbols
var mailOptions = {
    from: '"Our Code World " <[email protected]>', // sender address (who sends)
    to: '[email protected], [email protected]', // list of receivers (who receives)
    subject: 'Hello ', // Subject line
    text: 'Hello world ', // plaintext body
    html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }

    console.log('Message sent: ' + info.response);
});

Alternatively, if your account is hotmail instead of outlook, you can use the buil-in hotmail service using the following transport:

var transport = nodemailer.createTransport("SMTP", {
    service: "hotmail",
    auth: {
        user: "[email protected]",
        pass: "password"
    }
});

Have fun !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors