BugCoder BugCoder - 6 months ago 110
Node.js Question

Nodemailer is not able to send mail using aws on production server

I am getting the problem while sending mails through nodemailer and AWS. Following are the my settings :

var transporter = nodemailer.createTransport("SMTP", smtpTransport({
host: 'email-smtp.us-west-2.amazonaws.com',
service: 'SES',
port: 25,
secure: true,
debug: true,
auth: {
user: 'XXX',
pass: 'XXX'
}
}));


Mail sending code:

transporter.sendMail({
from: xxx@xxx.com,
to: xxx@xxx.com,
subject: 'XXX',
html: 'XXX'
}, function(error, response) {
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
});


I am getting following error:

{ [Error: connect ECONNREFUSED 127.0.0.1:25]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 25,
stage: 'init' }


Here's what my transporter object look like when I printed it :

Transport {
options:
SMTPTransport {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
options:
{ host: 'email-smtp.us-east-1.amazonaws.com',
service: 'SES',
port: 465,
secure: true,
debug: true,
auth: [Object] },
logger: { info: [Function], debug: [Function], error: [Function] },
name: 'SMTP',
version: '2.4.1[client:2.3.1]',
maxConnections: 5,
maxMessages: Infinity },
transportType: 'SMTP',
dkimOptions: false,
transport:
SMTPTransport {
options:
SMTPTransport {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
options: [Object],
logger: [Object],
name: 'SMTP',
version: '2.4.1[client:2.3.1]',
maxConnections: 5,
maxMessages: Infinity },
pool:
SMTPConnectionPool {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
port: 25,
host: 'localhost',
options: [Object],
_connectionsAvailable: [],
_connectionsInUse: [Object],
_messageQueue: [Object],
_idgen: 2 } },
version: '0.3.35',
sendMail: [Function] }


As you can clearly see it is using different setting then defined by me.

I am unable to find the issue. Any help will be appreciated.

Answer

if you want to send mail through aws it is better to use aws-sdk module. usage is given below

var aws = require('aws-sdk');
module.exports = function (to, from, sub, body) {
  aws.config.update({
    accessKeyId: 'accessKeyId',
    secretAccessKey: 'secretAccessKey',
    region: 'region'
  });

// load AWS SES
  var ses = new aws.SES({apiVersion: 'latest'});


// this sends the email
  ses.sendEmail({
        Source: from,
        Destination: {
          ToAddresses: to
        },
        Message: {
          Subject: {
            Data: sub
          },
          Body: {
            Html: {
              Data: body
            }
          }
        }
      }
      , function (err, data) {
        if (err) {
          console.log(err);
        } else {
          console.log('Email sent:');
          console.log(data);
        }
      });
};

Hope it helps :)