-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
113 lines (96 loc) · 2.18 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
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
const serverless = require('serverless-http');
const express = require('express');
const AWS = require('aws-sdk');
const bodyParser = require('body-parser');
const config = require('./config/' + process.env.STAGE + '.json');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const ses = new AWS.SES({
region: config.region
});
app.get('/', (req, res) => {
res.send('AWS SES - Email Webservice');
});
/**
* Create a new SES Template based on the request data
*/
app.post('/template', (req, res) => {
const { templateName, subject, body } = req.body;
var params = {
Template: {
TemplateName: templateName,
HtmlPart: body,
SubjectPart: subject
}
};
ses.createTemplate(params, function(err, data) {
if (err) {
res.send(err);
}else{
res.send(200);
}
});
});
/**
* Update an SES Template based on the request data
*/
app.put('/template', (req, res) => {
const { templateName, subject, body } = req.body;
var params = {
Template: {
TemplateName: templateName,
HtmlPart: body,
SubjectPart: subject
}
};
ses.updateTemplate(params, function(err, data) {
if (err) {
res.send(err);
}else{
res.send(200);
}
});
});
/**
* Delete an SES Template based on the request data
*/
app.delete('/template', (req, res) => {
const { templateName } = req.body;
var params = {
Template: {
TemplateName: templateName
}
};
ses.deleteTemplate(params, function(err, data) {
if (err) {
res.send(err);
}else{
res.send(200);
}
});
});
/**
* Send Email via AWS SES using the request Template and data
*/
app.post('/send-email', (req, res) => {
const { templateName, sendTo } = req.body;
const params = {
Template: templateName,
Destination: {
ToAddresses: [
sendTo
]
},
Source: config.SENDER_EMAIL,
TemplateData: JSON.stringify({})
};
ses.sendTemplatedEmail(params, function(err, data) {
if (err) {
res.send(err);
}else{
res.send(200);
}
});
});
module.exports.handler = serverless(app);