forked from jjenkins/node-amazon-ses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
223 lines (201 loc) · 6.12 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var _ = require('underscore');
var crypto = require('crypto');
var qs = require('querystring');
var request = require('request');
var xml = require('xml2js');
var AmazonSES = (function() {
var init = function(key, secret) {
this.accessKeyId = key;
this.secretAccessKey = secret;
};
var hmac = function(key, str) {
var hash = crypto.createHmac('sha256', key);
return hash.update(str).digest('base64');
};
var getDestinationList = function(addresses, type) {
var list = {};
_.each(addresses, function(address, idx) {
var key = 'Destination.'+ type +'Addresses.member.' + (idx + 1);
list[key] = address;
});
return list;
};
var getReplyToList = function(addresses) {
var list = {};
_.each(addresses, function(address, idx) {
var key = 'ReplyToAddresses.member.' + (idx + 1);
list[key] = address;
});
return list;
};
var buildMessage = function(opts) {
var params = {
'Action': 'SendEmail'
, 'Source': opts.from
, 'Message.Body.Text.Data': opts.body.text
, 'Message.Body.Text.Charset': 'UTF-8'
, 'Message.Body.Html.Data': opts.body.html
, 'Message.Body.Html.Charset': 'UTF-8'
, 'Message.Subject.Data' : opts.subject
};
_.extend(params, getDestinationList(opts.to, 'To'));
_.extend(params, getDestinationList(opts.cc, 'Cc'));
_.extend(params, getDestinationList(opts.bcc, 'Bcc'));
_.extend(params, getReplyToList(opts.replyTo));
return params;
};
var call = function(opts) {
var host = 'email.us-east-1.amazonaws.com';
var path = '/';
var now = (new Date()).toUTCString();
var body = qs.stringify(opts.query);
var authorization =
'AWS3-HTTPS '
+ 'AWSAccessKeyId=' + this.accessKeyId
+ ',Algorithm=HmacSHA256'
+ ',Signature=' + hmac(this.secretAccessKey, now)
var headers = {
'Date': now
, 'Host': host
, 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
, 'Content-Length': body.length
, 'X-Amzn-Authorization': authorization
};
var options = {
method: 'POST'
, uri: 'https://' + host + path
, headers: headers
, body: body
};
request(options, function(error, response, body) {
var parser = new xml.Parser();
parser.addListener('end', function(data) {
var err = null;
if (data.hasOwnProperty('Error')) {
err = new Error(data.Error.Message);
}
opts.callback(err, data);
});
parser.parseString(body);
});
};
var Constructor = function(accessKeyId, secretAccessKey) {
init(accessKeyId, secretAccessKey);
};
Constructor.prototype = {
constructor: AmazonSES,
/**
* Verify an email address. This action causes a confirmation email
* message to be sent to the specified address.
*
* @params {String} email The email address to be verified
* @params {Function} callback
* @throws {Error}
*/
verifyEmailAddress: function(email, callback) {
call({
query: {'Action': 'VerifyEmailAddress', 'EmailAddress': email}
, callback: function(err, data) {
if (err) throw err;
if (callback) {
callback(data.ResponseMetadata);
}
}
});
},
/**
* Return a list containing all of the email addresses that have been
* verified.
*
* @params {Function} callback
* @throws {Error}
*/
listVerifiedEmailAddresses: function(callback) {
call({
query: {'Action': 'ListVerifiedEmailAddresses'}
, callback: function(err, data) {
if (err) throw err;
var result = data.ListVerifiedEmailAddressesResult;
if (callback) {
callback(result.VerifiedEmailAddresses.member);
} else {
throw Error("Must pass listVerifiedEmailAddresses a callback function");
}
}
});
},
/**
* Delete the specified email address from the list of verified
* addresses.
*
* @params {String} email The email address to delete
* @params {Function} callback
*/
deleteVerifiedEmailAddress: function(email, callback) {
call({
query: {'Action': 'DeleteVerifiedEmailAddress', 'EmailAddress': email}
, callback: function(err, data) {
if (err) throw err;
if (callback) {
callback(data.ResponseMetadata);
}
}
});
},
/**
* Return the user's current sending limits.
*
* @params {Function} callback
*/
getSendQuota: function(callback) {
call({
query: {'Action': 'GetSendQuota'}
, callback: function(err, data) {
if (err) throw err;
if (callback) {
callback(data.GetSendQuotaResult);
} else {
throw Error("Must pass getSendQuota a callback function");
}
}
});
},
/**
* Return the user's sending statistics. The result is a list of data
* points, representing the last two weeks of sending activity. Each
* data point in the list contains statistics for a 15-minute interval.
*
* @params {Function} callback
*/
getSendStatistics: function(callback) {
call({
query: {'Action': 'GetSendStatistics'}
, callback: function(err, data) {
if (err) throw err;
if (callback) {
callback(data.GetSendStatisticsResult);
} else {
throw Error("Must pass getSendStatistics a callback function");
}
}
});
},
/**
* Composes an email message based on input data, and then immediately
* queues the message for sending.
*
* @params {Object} message The email message components
* @params {Function} callback
*/
send: function(message, callback) {
call({
query: buildMessage(message)
, callback: function(err, data) {
if (callback) callback(err, data);
}
});
}
};
return Constructor;
}());
module.exports = AmazonSES;