-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSesSender.js
More file actions
72 lines (58 loc) · 1.72 KB
/
SesSender.js
File metadata and controls
72 lines (58 loc) · 1.72 KB
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
var fs = require('fs'),
async = require('async'),
AWS = require('aws-sdk'),
proxyAgent = require('proxy-agent'),
path = require('path');
function SesSender(opts) {
var credentialsFilePath = './ses-credentials.json';
if (opts.config) {
credentialsFilePath = opts.config;
}
if (fs.existsSync(credentialsFilePath)) {
AWS.config.loadFromPath(credentialsFilePath);
} else if (fs.existsSync(path.join(process.cwd(), credentialsFilePath))) {
AWS.config.loadFromPath(path.join(process.cwd(), credentialsFilePath));
} else {
console.warn('Warning: Can not find credentials file.')
}
var proxy = process.env.https_proxy;
proxy = opts.proxy ? opts.proxy : proxy;
if (proxy) {
console.log('Using https proxy', proxy);
AWS.config.update({
httpOptions: {
agent: proxyAgent(proxy, true)
}
});
}
this.messageQueue = async.queue(this.processClient.bind(this), 1);
}
SesSender.prototype = {
queue: function(client) {
this.messageQueue.push(client);
},
processClient: function(client, callback) {
this.sendRaw(client, callback);
},
sendRaw: function(client, callback) {
var ses = new AWS.SES();
var options = {
RawMessage: {
Data: client.data
},
Destinations: client.to
};
console.log('Attempting to send SES message');
ses.sendRawEmail(options, function(err, data) {
if (err) {
console.error('Error sending SES email', err);
console.error(err.stack);
console.log(this.httpResponse && this.httpResponse.body && this.httpResponse.body.toString());
} else {
console.log('Successfully sent SES email.', data);
}
callback();
});
}
}
module.exports = SesSender;