-
Notifications
You must be signed in to change notification settings - Fork 37
/
email.js
298 lines (253 loc) · 6.87 KB
/
email.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'use strict';
const aws4 = require('aws4');
const debug = require('debug')('node-ses');
const parse = require('url').parse;
const querystring = require('querystring');
const request = require('request');
const xml2js = require('xml2js');
const xmlParser = new xml2js.Parser({explicitArray:false,mergeAttrs:true});
const SEND_EMAIL_ACTION = 'SendEmail';
const SEND_RAW_EMAIL_ACTION = 'SendRawEmail';
/**
* Email constructor.
*
* @param {Object} options
**/
function Email (options) {
this.action = options.action || SEND_EMAIL_ACTION;
this.key = options.key;
this.secret = options.secret;
this.amazon = options.amazon;
this.from = options.from;
this.subject = options.subject;
this.message = options.message;
this.altText = options.altText;
this.rawMessage = options.rawMessage;
this.configurationSet = options.configurationSet;
this.messageTags = options.messageTags;
this.extractRecipient(options, 'to');
this.extractRecipient(options, 'cc');
this.extractRecipient(options, 'bcc');
this.extractRecipient(options, 'replyTo');
}
/**
* Adds to, cc, and bcc fields to `data`.
*
* @param {Object} data
* @return data
**/
Email.prototype.addDestination = function (data) {
this.to.forEach(function (to, i) {
data['Destination.ToAddresses.member.' + (i + 1)] = to;
});
this.cc.forEach(function (to, i) {
data['Destination.CcAddresses.member.' + (i + 1)] = to;
});
this.bcc.forEach(function (to, i) {
data['Destination.BccAddresses.member.' + (i + 1)] = to;
});
return data;
};
/**
* Adds ConfigurationSetName and Tags to `data`
*
* @param {Object} data
* @return data
**/
Email.prototype.addSESHeaders = function (data) {
if (this.configurationSet) {
data['ConfigurationSetName'] = this.configurationSet;
}
if (this.messageTags) {
this.messageTags.forEach(function (tagSpec, i) {
data['Tags.member.' + (i + 1) + '.Name'] = tagSpec.name;
data['Tags.member.' + (i + 1) + '.Value'] = tagSpec.value;
});
}
return data;
};
/**
* Adds subject, alt text, and message body to `data`.
*
* @param {Object} data
* @return data
**/
Email.prototype.addMessage = function (data) {
if (this.subject) {
data['Message.Subject.Data'] = this.subject;
data['Message.Subject.Charset'] = 'UTF-8';
}
if (this.message) {
data['Message.Body.Html.Data'] = this.message;
data['Message.Body.Html.Charset'] = 'UTF-8';
}
if (this.altText) {
data['Message.Body.Text.Data'] = this.altText;
data['Message.Body.Text.Charset'] = 'UTF-8';
}
return data;
};
/**
* Adds the list of ReplyTos to `data`.
*
* @param {Object} data
* @return data
**/
Email.prototype.addReplyTo = function (data) {
this.replyTo.forEach(function(to, i) {
data['ReplyToAddresses.member.' + (i + 1)] = to;
});
return data;
};
/**
* Prepares param object for the AWS request.
*
* @return {Object}
*/
Email.prototype.data = function () {
var data = {
Action: this.action
, AWSAccessKeyId: this.key
, Source: this.from
};
// recipients and reply tos
data = this.addDestination(data);
data = this.addReplyTo(data);
// SES Event Publishing Information
data = this.addSESHeaders(data);
// message payload
if (this.action === SEND_EMAIL_ACTION) {
data = this.addMessage(data);
} else if (this.action === SEND_RAW_EMAIL_ACTION) {
data['RawMessage.Data'] = Buffer.from(this.rawMessage).toString('base64');
}
return data;
};
/**
* Extracts recipients from options.
*
* @param {String} prop - either to,cc,bcc,replyTo
* @param {Object} options
*/
Email.prototype.extractRecipient = function (options, prop) {
if (options[prop]) {
this[prop] = Array.isArray(options[prop]) ? options[prop] : [options[prop]];
} else {
this[prop] = [];
}
};
/**
* Creates required AWS headers.
*
* No additional custom headers by default.
*
* @return Object
**/
Email.prototype.headers = function () {
var headers = {};
return headers;
};
/**
* Sends the email.
*
* @param {Function} callback
* @api Public
**/
Email.prototype.send = function send (callback) {
const invalid = this.validate();
if (invalid) {
return callback(new Error(invalid));
}
// Prepare the data and send to it AWS SES REST API
var data = querystring.stringify(this.data());
var parsedUrl = parse(this.amazon);
var headers = this.headers();
var options = {
uri: this.amazon
, host: parsedUrl.hostname
, headers: headers
, body: data
, service: 'ses'
};
var credentials = this.key && this.secret && {
accessKeyId : this.key,
secretAccessKey : this.secret
};
var signedOpts = aws4.sign(options, credentials);
debug('posting: %j', signedOpts);
request(signedOpts, (err, res, data) => this._processResponse(err, res, data, callback));
};
/**
* Process a response from the 'request' call used to POST email to SES
* @private
*
*/
Email.prototype._processResponse = function _processResponse (err, res, data, callback) {
debug('received: %s %d %j', err, res && res.statusCode || 0, data);
if (err) {
return callback({
Type : "NodeSesInternal",
Code : "RequestError",
Message: err,
}, data, res);
}
if (res.statusCode < 200 || res.statusCode >= 400) {
return xmlParser.parseString(data,function(err,result){
if(err) {
return callback({
Type: "NodeSesInternal",
Code: "ParseError",
Message: err
});
}
// Return an error object with keys of Type, Code and Message
// Reference docs at: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/query-interface-responses.html
if(result && result.ErrorResponse){
return callback(result.ErrorResponse.Error)
}
// Once during an S3 outage AWS returned valid XML that didn't match the usual error structure.
// We cover this unlikely case
else {
return callback({
Type : "NodeSesInternal",
Code : "XmlError",
Message: result,
})
}
});
} else {
return callback(null, data, res);
}
};
/**
* Validates the input.
*
* @returns {String|Undefined}
**/
Email.prototype.validate = function () {
if (this.action === SEND_EMAIL_ACTION) {
if (!this.to.length && !this.cc.length && !this.bcc.length) {
return 'To, Cc or Bcc is required';
}
if (!(this.subject && this.subject.length)) {
return 'Subject is required';
}
}
if (this.action === SEND_RAW_EMAIL_ACTION) {
if (!(this.rawMessage && this.rawMessage.length)) {
return 'Raw message is required';
}
}
// all actions require the following
if (!(this.from && this.from.length)) {
return 'From is required';
}
};
/**
* Exports
**/
exports.Email = Email;
exports.actions = {
SendEmail : SEND_EMAIL_ACTION,
SendRawEmail : SEND_RAW_EMAIL_ACTION
};