-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (58 loc) · 1.58 KB
/
index.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
const fp = require('fastify-plugin');
const AWS = require('aws-sdk');
module.exports = fp(function(fastify, opts, next) {
if (!opts.config || typeof opts.config !== 'object') {
return next(new Error('You need to provide config.'));
}
const config = opts.config;
if (typeof config.region === 'undefined') {
return next(new Error('Region is not provided.'));
}
AWS.config.update({region: config.region});
fastify.decorate('ses', {
async send(params) {
if (typeof params.to !== 'string') {
throw new Error('Invalid param: to');
}
if (typeof params.from !== 'string') {
throw new Error('Invalid param: from');
}
if (typeof params.subject !== 'string') {
throw new Error('Invalid param: subject');
}
if (typeof params.text !== 'string') {
throw new Error('Invalid param: text');
}
var sesParams = {
Destination: {
ToAddresses: [
params.to
],
},
Message: {
Body: {
Text: {
Charset: "UTF-8",
Data: params.text
},
},
Subject: {
Charset: "UTF-8",
Data: params.subject
}
},
Source: params.from,
};
// TODO: Add additional params
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(sesParams).promise();
return sendPromise.then(
function(data) {
return data.MessageId;
}).catch(
function(err) {
throw err;
});
},
});
next();
});