[node.js] Sending emails in Node.js?

I recently started programming my first node.js. However, I discovered that I am unable to create a contact me form that sends straight to my email since I can't find any modules from node that is able to send emails.

Does anyone know of a node.js email library or an sample contact form script?

This question is related to node.js email-integration

The answer is


Check out emailjs

After wasting lots of time on trying to make nodemailer work with large attachments, found emailjs and happy ever since.

It supports sending files by using normal File objects, and not huge Buffers as nodemailer requires. Means that you can link it to, f.e., formidable to pass the attachments from an html form to the mailer. It also supports queueing..

All in all, no idea why nodejitsu ppl chose nodemailer to base their version on, emailjs is just much more advanced.


You definitely want to use https://github.com/niftylettuce/node-email-templates since it supports nodemailer/postmarkapp and has beautiful async email template support built-in.


Mature, simple to use and has lots of features if simple isn't enought: Nodemailer: https://github.com/andris9/nodemailer (note correct url!)


Nodemailer is basically a module that gives you the ability to easily send emails when programming in Node.js. There are some great examples of how to use the Nodemailer module at http://www.nodemailer.com/. The full instructions about how to install and use the basic functionality of Nodemailer is included in this link.

I personally had trouble installing Nodemailer using npm, so I just downloaded the source. There are instructions for both the npm install and downloading the source.

This is a very simple module to use and I would recommend it to anyone wanting to send emails using Node.js. Good luck!


You could always use AlphaMail (disclosure: I'm one of the developers behind it).

Just install with NPM:

npm install alphamail

Sign up for a AlphaMail account. Get a token, and then you can start sending with the AlphaMail service.

var alphamail = require('alphamail');

var emailService = new alphamail.EmailService()
    .setServiceUrl('http://api.amail.io/v1/')
    .setApiToken('YOUR-ACCOUNT-API-TOKEN-HERE');

var person = {
    id: 1234,
    userName: "jdoe75",
    name: {
        first: "John",
        last: "Doe"
    },
    dateOfBirth: 1975
};

emailService.queue(new alphamail.EmailMessagePayload()
    .setProjectId(12345) // ID of your AlphaMail project (determines template, options, etc)
    .setSender(new alphamail.EmailContact("Sender Company Name", "[email protected]"))
    .setReceiver(new alphamail.EmailContact("John Doe", "[email protected]"))
    .setBodyObject(person) // Any serializable object
);

And in the AlphaMail GUI (Dashboard) you'll be able to edit the template with the data you sent:

<html>
    <body>
        <b>Name:</b> <# payload.name.last " " payload.name.first #><br>
        <b>Date of Birth:</b> <# payload.dateOfBirth #><br>

        <# if (payload.id != null) { #>
            <a href="http://company.com/sign-up">Sign Up Free!</a>
        <# } else { #>
            <a href="http://company.com/login?username=<# urlencode(payload.userName) #>">Sign In</a>
        <# } #>
    </body>
</html>

The templates are written in Comlang, it's a simple template language specifically designed for emails.


campaign is a comprehensive solution for sending emails in Node, and it comes with a very simple API.

You instance it like this.

var client = require('campaign')({
  from: '[email protected]'
});

To send emails, you can use Mandrill, which is free and awesome. Just set your API key, like this:

process.env.MANDRILL_APIKEY = '<your api key>';

(if you want to send emails using another provider, check the docs)

Then, when you want to send an email, you can do it like this:

client.sendString('<p>{{something}}</p>', {
  to: ['[email protected]', '[email protected]'],
  subject: 'Some Subject',
  preview': 'The first line',
  something: 'this is what replaces that thing in the template'
}, done);

The GitHub repo has pretty extensive documentation.


@JimBastard's accepted answer appears to be dated, I had a look and that mailer lib hasn't been touched in over 7 months, has several bugs listed, and is no longer registered in npm.

nodemailer certainly looks like the best option, however the url provided in other answers on this thread are all 404'ing.

nodemailer claims to support easy plugins into gmail, hotmail, etc. and also has really beautiful documentation.


Nodemailer Module is the simplest way to send emails in node.js.

Try this sample example form: http://www.tutorialindustry.com/nodejs-mail-tutorial-using-nodemailer-module

Additional Info: http://www.nodemailer.com/


npm has a few packages, but none have reached 1.0 yet. Best picks from npm list mail:

[email protected]
[email protected]
[email protected]

Complete Code to send Email Using nodemailer Module

var mailer = require("nodemailer");

// Use Smtp Protocol to send Email
var smtpTransport = mailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "[email protected]",
        pass: "gmail_password"
    }
});

var mail = {
    from: "Yashwant Chavan <[email protected]>",
    to: "[email protected]",
    subject: "Send Email Using Node.js",
    text: "Node.js New world for me",
    html: "<b>Node.js New world for me</b>"
}

smtpTransport.sendMail(mail, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    smtpTransport.close();
});