-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
47 lines (44 loc) · 1.3 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
"use strict";
var config = require('./config.json'),
qs = require('qs'),
nodeMailer = require('nodemailer'),
AWS = require('aws-sdk'),
sesTransport = require('nodemailer-ses-transport');
var ses = new AWS.SES();
var transporter = nodeMailer.createTransport(sesTransport({
ses: ses
}));
module.exports.contact = function (e, context, callback) {
var redirect = config.redirect;
var origin = config.origin;
const errorResponse = {
statusCode: 500,
body: 'Could not send message'
};
const successResponse = {
statusCode: 302,
headers: {
'Access-Control-Allow-Origin': origin,
'Location': redirect
},
body: 'Redirecting you back to ' + redirect
};
var text = JSON.stringify(qs.parse(e.body), null, 2);
text += "\n request: ";
text += JSON.stringify(qs.parse(e.requestContext), null, 2);
var options = {
from: config.from,
to: config.to,
subject: 'Contact Form',
text: text
};
var on_done = function (error, info) {
if (error) {
console.log(error);
return callback(null, errorResponse);
}
console.log(info);
callback(null, successResponse);
};
var r = transporter.sendMail(options, on_done);
};