Sending email in Node JS with Amazon SES in less than 10 minutes

NodeMailer

Sending e-mails with nodemailer requires almost no effort. NodeMailer features a bunch of possible transports as SMTP, XOAUTH, Gmail, sendmail, Pickup and many others. You can even build your own transport handler.

First of all you need to install it.

npm install nodemailer --save  

Check out the docs and

The Code

Once Nodemailer is installer, you can require the module as nodemailer

var nodemailer = require("nodemailer");  

You will need to define the transport (for us is SMTP) and mail options.

var nodemailer = require("nodemailer");


exports.submit = function (req, res) {

    var transport = nodemailer.createTransport("SMTP", { // Yes. SMTP!
        host: "email-smtp.eu-west-1.amazonaws.com", // Amazon email SMTP hostname
        secureConnection: true, // use SSL
        port: 465, // port for secure SMTP
        auth: {
            user: "Your Amazon SMTP User", // Use from Amazon Credentials
            pass: "Your Amazon SMTP Pass" // Use from Amazon Credentials
        }
    });

    var mailOptions = {
        from: "Gabriel Manolache <gmanolache@domain.com>", // sender address
        to: "Gabriel Manolache <gmanolache@domain.com>", // list of receivers
        subject: "User registerd", // Subject line
        html: "<b>New user registered!</b>" // email body
    };

    // send mail with defined transport object
    transport.sendMail(mailOptions, function(error, response){
        if(error){
            console.log(error);
        }else{
            console.log("Message sent: " + response.message);
        }

        transport.close(); // shut down the connection pool, no more messages
    });

    res.send('OK');
};

Setting up SMTP with Amazon SES

First of all, if you don't have a Amazon Web Services account you can go and register for free for the first year. You will only pay what you use. Link

Log into Amazon SES on your region and Create your SMTP Credeantials. Do not share them with anyone! If your Credentials were compromised, be sure to rotate them.

Testing

Go to Email Addresses and verify your email address. Once you have that verified you can send yourself an email. Note that in "sandbox" mode you can only send from and to verified senders under a limit of 200 emails per day.

Going for production

If you need to increase your limits or be able to send emails to users you will need Production Access in Amazon SES. Apply for it and you should hear back from the Amazon support team in max 24h.

When activating Production Access and sending emails to users, be sure to build an Unsubscribe feature in your app. Also when sending emails you should comply with your country's law and legislation.

Security considerations

I don't suggest using NodeMailer's SES transport as it requires your Amazon User account not the SMTP account.
If compromised, an attacker could gain access to your entire cloud not only the STMP access.

Try it out. You'll be sending emails to happiest of users in no time :D