Permalink
Browse files

Adding some fixes to the SES library

  • Loading branch information...
David Ellis
David Ellis committed Aug 12, 2011
1 parent fae47ee commit cf9ffc22bb8d8ad41d91737fca056a772d025b32
Showing with 9 additions and 8 deletions.
  1. +9 −8 lib/engines/SES.js
View
@@ -15,29 +15,28 @@ function ISODateString(d){
//Extracts the hostname from a given URL
function getHostname(url) {
- return url.replace(/^https?:\/\/([^\/]*)\/.*$/, "$1");
+ return url.replace(/^https?:\/\/([^\/]*).*$/, "$1");
}
//Custom keyValPair construction that ignores "special" config keys
function buildKeyValPairs(config) {
var keyValPairs = [];
for(var key in config) {
if(key != "ServiceUrl" && key != "AWSSecretKey") {
- keyValPairs.push((key + "=" + config[key]));
+ keyValPairs.push((encodeURIComponent(key) + "=" + encodeURIComponent(config[key])));
}
}
return keyValPairs.join("&");
}
//Following the SES.pm signature implementation since Amazon's documentation sucks
function buildV2Signature(config) {
- var sha256 = crypto.createHash('sha256');
+ var sha256 = crypto.createHash('sha256', config.AWSSecretKey);
sha256.update(
- "POST\n" +
+ "GET\n" +
getHostname(config.ServiceUrl) +
"\n/\n" +
- buildKeyValPairs(config) +
- config.AWSSecretKey
+ buildKeyValPairs(config)
);
return sha256.digest('base64');
}
@@ -81,18 +80,20 @@ exports.send = function(emailMessage, config, callback) {
emailMessage.prepareVariables();
email = emailMessage.generateHeaders() + "\r\n\r\n" + emailMessage.generateBody();
//Unchangeable configuration settings for SES
+ delete config.instanceId;
config.Version = '2010-12-01';
config.SignatureVersion = '2';
config.SignatureMethod = 'HmacSHA256';
config.Action = 'SendRawEmail';
- config['RawMessage.Data'] = email;
+ config['RawMessage.Data'] = (new Buffer(email)).toString('base64');
config.Signature = buildV2Signature(config);
//Construct the http/https request object
path += buildKeyValPairs(config);
hostname = getHostname(config.ServiceUrl);
var reqObj = {hostname: hostname, path: path};
+ console.log(reqObj);
//Execute the request on the correct protocol
- if(/^https:/.test(config.ServiceUrl) {
+ if(/^https:/.test(config.ServiceUrl)) {
https.get(reqObj, buildResponseHandler(callback));
} else {
http.get(reqObj, buildResponseHandler(callback));

0 comments on commit cf9ffc2

Please sign in to comment.