forked from simalexan/api-lambda-send-email-ses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (52 loc) · 1.93 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
const AWS = require('aws-sdk'),
SES = new AWS.SES(),
processResponse = require('./process-response.js'),
FROM_EMAIL = process.env.FROM_EMAIL,
UTF8CHARSET = 'UTF-8';
exports.handler = (event) => {
if (event.httpMethod === 'OPTIONS') {
return Promise.resolve(processResponse(true));
}
if (!event.body) {
return Promise.resolve(processResponse(true, 'Please specify email parameters: toEmails, subject, and message ', 400));
}
const emailData = JSON.parse(event.body);
if (!emailData.toEmails || !Array.isArray(emailData.toEmails) || !emailData.subject || !emailData.message) {
return Promise.resolve(processResponse(true, 'Please specify email parameters: toEmails, subject and message', 400));
}
const destination = {
ToAddresses: emailData.toEmails
}
if (emailData.ccEmails) {
destination.CcAddresses = emailData.ccEmails;
}
const body = (emailData.message && isHTML(emailData.message)) ?
{ Html: { Charset: UTF8CHARSET, Data: emailData.message } } :
{ Text: { Charset: UTF8CHARSET, Data: emailData.message } };
const emailParams = {
Destination: destination,
Message: {
Body: body,
Subject: {
Charset: UTF8CHARSET,
Data: emailData.subject
}
},
Source: FROM_EMAIL
};
if (emailData.replyToEmails && Array.isArray(emailData.replyToEmails)) {
emailParams.ReplyToAddresses = emailData.replyToEmails;
}
return SES.sendEmail(emailParams).promise()
.then(() => (processResponse(true)))
.catch(err => {
console.error(err, err.stack);
const errorResponse = `Error: Execution update, caused a SES error, please look at your logs.`;
return processResponse(true, errorResponse, 500);
});
};
function isHTML(value) {
value = value.trim();
return value.startsWith('<') && value.endsWith('>') &&
(value.includes('<body') || value.includes('<div') || value.includes('<s') || value.includes('<h') || value.includes('<p'));
}