angvee angvee - 3 months ago 65
Node.js Question

AWS SES send email not working

Actually, I'm newbie to use AmazonWebService(AWS) but in my case, I must create some program with node.js to send a email use AWS SES node.js

I had read all AWS SES documentation, but I don't really understand. Some blog wrote tutorial and I had follow their code but in my code it did'nt work.

I have two sample code that I was wrote. Here first code :

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

var transporter = nodemailer.createTransport(ses({
accessKeyId: 'xxxxxxxxxxxxxxxxxxx',
secretAccessKey: 'xxxxxxxxxxxxxxxxxxxxxxxxx'
}));

transporter.sendMail({
from: 'avi.adhi@vasww.com',
to: 'gengar@gmail.com',
subject: 'My Amazon SES Simple Email',
text: 'Amazon SES is cool'
});


Above is code referrence from this http://budiirawan.com/send-emails-using-amazon-ses-and-node-js/

And second is like this :

var aws = require('aws-sdk');

var ses = new aws.SES({
accessKeyId: 'xxxxxxxxxxxxxxxxxxx',
secretAccesskey: 'xxxxxxxxxxxxxxxxxxxxxxxx',
region: 'us-west-2'
});

// send to list
var to = ['gengar@gmail.com'];

// this must relate to a verified SES account
var from = 'avi.adhi@vasww.com';


// this sends the email
// @todo - add HTML version
ses.sendEmail( {
Source: from,
Destination: { ToAddresses: to },
Message: {
Subject: {
Data: 'A Message To You'
},
Body: {
Text: {
Data: 'Stop your messing around',
}
}
}
}
, function(err, data) {
if(err) throw err
console.log('Email sent:');
console.log(data);
});


The second code referrence from http://timstermatic.github.io/blog/2013/08/14/sending-emails-with-node-dot-js-and-amazon-ses/.
The second code give me error message :

C:\xampp\htdocs\mail\node_modules\aws-sdk\lib\request.js:31
throw err;
^
Error: connect ENETUNREACH :80
at Object.exports._errnoException (util.js:856:11)
at exports._exceptionWithHostPort (util.js:879:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)


Both does'nt work at all, I had make sure my AWS access key are correct and my avi.adhi@avasww.com had been verified by AWS SES. I'm very appriciate if someone can solve this issue.

Answer

I've found out how to solve this case. The first code actually works, I just forgot to include AWS Region in SES declare and we should got verified email from AWS. here is my new code and it work well. thanks

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

var transporter = nodemailer.createTransport(ses({
    accessKeyId: 'xxxxxxxxxxxxxxxxxxx',
    secretAccessKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    region: 'us-west-2' 
}));

transporter.sendMail({
    from: 'your_verified_email@xxxx.com',
    to: 'target_email@xxxx.com',
    subject: 'Email Testing',
    html: '<h1>Title</h1>',
    attachments: [
        {
          filename: 'report',
          path: 'C:\\xampp\\htdocs\\js\\report.xlsx',
          contentType: 'application/vnd.ms-excel'
        }
    ]
}
, function(err, data) {
    if(err) throw err
        console.log('Email sent:');
        console.log(data);
 });

That code use send a attachment too, hope it will help someone who get same case.

Comments