-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler.js
68 lines (62 loc) · 1.93 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
61
62
63
64
65
66
67
68
"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 referrer = config.website;
const error_response = {
statusCode: 500,
body: 'Could not send message'
};
const response = {
statusCode: 301,
headers: {
'Location': referrer + "?sent=true"
},
body: 'Redirecting you back to ' + referrer
}
var body = qs.parse(e.body),
required = [ "name", "message", "email" ],
options = {},
i = null,
r = null,
text = null,
on_done = null;
// make sure we have all the needed fields
for (i = 0; i < required.length; i += 1) {
r = required[i];
if (!body.hasOwnProperty(r)) {
console.log("missing data for " + r)
return callback(null, error_response);
}
}
text = "Name: " + body.name + "\n";
if (body.hasOwnProperty("affiliation") && body.affiliation.length > 0) {
text += "Affiliation: " + body.affiliation + "\n";
}
text += "Email: " + body.email + "\n";
text += "Source IP: " + e.requestContext.identity.sourceIp + "\n";
text += "User Agent: " + e.requestContext.identity.userAgent + "\n";
text += "\n\n\n";
text += body.message;
options = {
from: config.from,
to: config.to,
replyTo: body.email,
subject: 'Contact Form [' + body.name + ']',
text: text
};
on_done = function (error, info) {
if (error) {
console.log(error)
return callback(null, error_response);
}
console.log(info);
callback(null, response);
};
r = transporter.sendMail(options, on_done);
};