-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
60 lines (52 loc) · 1.39 KB
/
handler.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
'use strict';
var EmailService = require('./EmailService')
var Handlebars = require('handlebars');
function createEmailService(smtp) {
const emailService = new EmailService(
smtp.address,
smtp.username,
smtp.password,
smtp.port
)
return emailService
}
module.exports.notifier = (event, context, callback) => {
// Parse email content variables
const variables = JSON.parse(event.variables)
// Fill template with variables
var template = Handlebars.compile(event.content);
var templateContent = template(variables);
// Fill subject with variables
var subject = Handlebars.compile(event.subject);
var templateSubject = subject(variables);
// Parse email deliver options
const opt = JSON.parse(event.options)
// Parse smtp settings
var smtp = JSON.parse(event.smtp);
// Send Email
const emailService = createEmailService(smtp)
var service = emailService.sendEmail(
opt.email_from,
opt.email_to,
templateSubject,
templateContent,
opt.cc,
opt.bcc,
opt.reply_to
).then((info) => {
let response = {
statusCode: 200,
headers: { 'Access-Control-Allow-Origin': '*' },
body: info
};
callback(null, response)
})
.catch(error => {
let response = {
statusCode: 422,
headers: { 'Access-Control-Allow-Origin': '*' },
body: error
};
callback(null, response)
})
};