Reputation: 3660
How does one send an email through Amazon's SES service that has a custom header ? (i.e. I want to attach a file to the e-mail)
It seems like there is a JavaScript AWS SDK here - http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html, but including it through the browser doesn't work, because SES is not available (yet) and it seems too complicated to make it work with node and meteor on the backend.
Upvotes: 0
Views: 322
Reputation: 308
You can include the aws .js, by creating your own minified version from AWS Builder. Include this .js file in your html page & you can refer my answer from here to invoke the sendRawEmail code from the javascript client side itself.
AWS.config.update({region: '<your_region>'});
AWS.config.update({apiVersion: '2010-12-01'});
var ses = new window.AWS.SES({"accessKeyId": "your_access_key", "secretAccessKey": "your_secret_key", "region": "<your_region>"});
Upvotes: 0
Reputation: 75955
It might be easier to use nodemailer
mrt add nodemailer
Server side code
var transport = nodemailer.createTransport("SES", {
AWSAccessKeyID: "AWSACCESSKEY",
AWSSecretKey: "AWS/Secret/key"
});
transport.sendMail({
to: '',
from: '',
headers : ['one', 'two'],
text: 'body here',
html: 'html here',
subject: 'subject'
attachments : [
{
filename: "Filename.jpg",
filePath: "<path to file on your server>"
}
]
}, function(err, result) {
console.log(err,result);
});
transport.close();
There's more details of what options you have available, (plenty of them!) at: https://github.com/andris9/Nodemailer
Upvotes: 1
Reputation: 3660
You are on the right track, but ultimately you would need to use the HTTP.call()
method on the server-side in order to create a POST/GET
request to Amazon's SES service.
First be sure to have the following packages installed for Meteor
through Meteorite
-
mrt add crypto-base
mrt add crypto-base64
mrt add crypto-hmac
mrt add crypto-md5
mrt add crypto-sha1
mrt add crypto-sha256
Also make sure you have the http
Meteor package meteor add http
.
Secondly get your AWS AccessKeyId
,secretAccessKey
combo - only the keyID
is shown on Amazon, if you don't remember the secretKey
you would just have to regenerate both of them.
var url = 'https://email.us-east-1.amazonaws.com',
awsAKID = '<Access Key ID goes here>',
secretAccessKey = '<Secret Access Key goes here>',
signature = '',
currentDate = new Date().toUTCString(),
xAmznHeader = '',
emailContent = '',
resultSend = '-1';
// Encrypt the currentDate with your secretAccessKey
// afterwards encode it in base64
// Note: that you can substitute the SHA256 with SHA1 for lower encryption
signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(currentDate, secretAccessKey));
// this is the POST request header that Amazon uses to verify the validity of your request
xAmznHeader = 'AWS3-HTTPS AWSAccessKeyId=' + awsAKID + ', Algorithm=HmacSHA256, Signature=' + signature;
emailContent =
'From: <your verified sender e-mail here>\n' +
'To: <wherever you want to send it>\n' +
'Date: ' + currentDate +'\n' +
'Subject: Hi There\n' +
'MIME-Version: 1.0\n' +
'Content-Type: multipart/mixed; boundary="someBoundaryNameHere"\n' +
'--someBoundaryNameHere\n' +
'Content-Transfer-Encoding: 7bit\n\n' +
// body starts here, SES wants you to seperate the header from the body
//with an empty line, notice the extra '\n' above
'Readable text here\n' +
'--someBoundaryNameHere\n' +
'Content-Type: application/xml;\n' +
xmlString +
'\r\n';
// now we base64 encode the whole message, that's how Amazon wants it
emailContent = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(emailContent));
// we can finally make the POST request
resultSend = HTTP.call("POST", url, {
params: {
Action : "SendRawEmail",
"RawMessage.Data" : emailContent
},
headers : {
'Date' : currentDate,
'X-Amzn-Authorization' : xAmznHeader
}
});
Upvotes: 0