Nodemailer and Amazon SES

O P picture O P · Jun 1, 2014 · Viewed 13.9k times · Source

My structure:

site
-- node_modules
---- nodemailer
-- webclient
---- js
------- controller.js

site/node_modules/nodemailer
site/webclient/js/controller.js

site/webclient/js/controller.js:

    var nodemailer = require('../../node_modules/nodemailer');

    var transport = nodemailer.createTransport('SES', {
        AWSAccessKeyID: 'xxx', // real one in code
        AWSSecretKey: 'xxx', // real one in code
        ServiceUrl: 'email-smtp.us-west-2.amazonaws.com'
    });

    var message = {
        from: '[email protected]', // verified in Amazon AWS SES
        to: '[email protected]', // verified in Amazon AWS SES
        subject: 'testing',
        text: 'hello',
        html: '<p><b>hello</b></p>' +
              'test'
    };

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

This code is part of a controller where all other functions within it work perfectly.

  • Is there something I am missing?
  • Perhaps I'm calling for the incorrect nodemailer module?
  • Or should the transport be SMTP, not SES?

I'm stuck.

Answer

Deepak Puthraya picture Deepak Puthraya · Aug 10, 2015

The below code is what I used and it worked for me. This is for the current NodeMailer. Where the transport module needs to be included separately. In the previous versions the transport modules were built in.

var nodemailer = require('nodemailer');
var sesTransport = require('nodemailer-ses-transport');

var SESCREDENTIALS = {
  accessKeyId: "accesskey",
  secretAccessKey: "secretkey"
};

var transporter = nodemailer.createTransport(sesTransport({
  accessKeyId: SESCREDENTIALS.accessKeyId,
  secretAccessKey: SESCREDENTIALS.secretAccessKey,
  rateLimit: 5
}));

var mailOptions = {
  from: 'FromName <[email protected]>',
  to: '[email protected]', // list of receivers
  subject: 'Amazon SES Template TesT', // Subject line
  html: < p > Mail message < /p> / / html body
};

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

UPDATE

The nodemailer library has been updated since I last wrote this answer. The correct way to use the library with AWS SES is provided in their docs. The link for a working example

Further Reading