Emailing in Node.js with Nodemailer

Emailing in Node.js is easy to do with the Nodemailer module. Nodemailer supports plain text content, HTML content, attachments, different transport methods, etc. The official documentation can be found on github.

Getting Started
Lets start off by creating our directory.

mkdir EmailExample
cd EmailExample

Creating package.json
In the EmailExample directory we just created, run npm init to automatically create your package.json file. You’ll be asked a series of questions about your app.

npm init

Once package.json has been created, open it up and add the dependency to nodemailer. Here’s a sample of what it should look like.

{
  "name": "EmailExample",
  "version": "1.0.0",
  "private": true,
  "description": "Sample email app",
  "main": "app.js",
  "scripts": {
    "start": "node app"
  },
  "author": "Jason Everett",
  "dependencies": {
    "nodemailer": "0.3.29"
  }
}

Once the dependency has been added, run npm install to have the nodemailer module downloaded and installed.

npm install

app.js
Next, we’ll create our javascript file to send emails.

var nodemailer = require("nodemailer");

var smtpTransport = nodemailer.createTransport("SMTP",{
   service: "Gmail",  // sets automatically host, port and connection security settings
   auth: {
       user: "[email protected]",
       pass: "gmailPassword"
   }
});

smtpTransport.sendMail({  //email options
   from: "Sender Name <[email protected]>", // sender address.  Must be the same as authenticated user if using Gmail.
   to: "Receiver Name <[email protected]>", // receiver
   subject: "Emailing with nodemailer", // subject
   text: "Email Example with nodemailer" // body
}, function(error, response){  //callback
   if(error){
       console.log(error);
   }else{
       console.log("Message sent: " + response.message);
   }
   
   smtpTransport.close(); // shut down the connection pool, no more messages.  Comment this line out to continue sending emails.
});

The first thing we do is declare our module dependency.

var nodemailer = require("nodemailer");

Next we create the SMPT transport.

var smtpTransport = nodemailer.createTransport("SMTP",{
   service: "Gmail",  // sets automatically host, port and connection security settings
   auth: {
       user: "[email protected]",
       pass: "gmailPassword"
   }
});

This example uses Gmail SMPT but you can use other services such as: Hotmail, iCloud, Yahoo, etc. Alternatively you can use your own SMPT sever if available. The drawback to using a service such as Gmail is that the From address must be the same email address used for authentication.

Last, we call the sendMail(mailOptions[, callback]) command from the transport object to send the email.

smtpTransport.sendMail({  //email options
   from: "Sender Name <[email protected]>", // sender address.  Must be the same as authenticated user if using GMail.
   to: "Receiver Name <[email protected]>", // receiver
   subject: "Emailing with nodemailer", // subject
   text: "Email Example with nodemailer" // body
}, function(error, response){  //callback
   if(error){
       console.log(error);
   }else{
       console.log("Message sent: " + response.message);
   }
   
   smtpTransport.close(); // shut down the connection pool, no more messages.  Comment this line out to continue sending emails.
});

Now all you need to do is run your app to send an email.

node app.js

You should now have a basic understanding of sending emails with nodemailer.

Source code: https://github.com/ijason/NodeJS-Email

13 Replies to “Emailing in Node.js with Nodemailer”

  1. Hi! First of all, thanks for great articles. Very helpful. I have a question 🙂
    I am using the code for sending email, and always have error like this “{ [Error: connect ETIMEDOUT] code: ‘ETIMEDOUT’, errno: ‘ETIMEDOUT’, syscall: ‘connect’ }”. Can you help me? Thanks…

      • nice it works , can you please show us how to send a formatted link with url and link( like when a user sign up or change a password )also with express-mailer thank you

  2. Thanks man. I found the problem 🙂 Somehow the tpl link modem on my work block the message sending. but at home all work just perfect. And I have one more question 🙂 May be you or you know the tutorial for registration user, I mean sending the verification email and other things. Thanks.

  3. And one more thing. In your opinion, what orm (module) the best way for using? I lookin you tutorial by creating employee… you use there a mongo, but what you think about Mongoose? Thanks.

  4. As mentioned through out the article i realized that the sender of the email has to be the same as that of the authenticated user for smtp (sender address. Must be the same as authenticated user if using GMail.). I even realized this with some tests I have been doing with nodemailer but just curious as to why they have to be the same and how should I go about allowing a different address being the sender of the email for example to be used in different aspects of my web application.

  5. Hi,
    First of all thank for your help. I am facing an problem. I would be really helpfull if you can suggest any workaround for this..

    I have implement code according to your article and now i am able to send email but i can do this from Command Prompt .

    I have to use this code in Protractor .
    I have my automation script written in Protractor (Webstorm Editor) . Now when i use your code in this script so this doesnt work means emails are not sending .
    I would be thankfull if u can help

  6. node mailer.js
    /home/…./web/node_modules/nodemailer/lib/nodemailer.js:228
    this.mailcomposer.setMessageOption(mailOptions);
    ^

    TypeError: this.mailcomposer.setMessageOption is not a function
    at Nodemailer.setGeneralOptions (/home/…/web/node_modules/nodemailer/lib/nodemailer.js:228:23)

    Which version must I install ?

  7. very nice article…
    But I have doubt basically… Is it compulsory to run this app in a particular server host to send the email?.. Please explain me..
    I have practicing this in Offline ( without hosting in server).

Leave a Reply

Your email address will not be published. Required fields are marked *

Please Do the Math      
 

*