forked from lexoyo/serverless-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (99 loc) · 3.24 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
'use strict';
var http = require('http');
var fs = require('fs');
var formidable = require("formidable");
var util = require('util');
var nodemailer = require('nodemailer');
// setup the server
// listen on port specified by the `PORT` env var
var server = http.createServer(function (req, res) {
if (req.method.toLowerCase() == 'get') {
displayForm(res);
} else if (req.method.toLowerCase() == 'post') {
//processAllFieldsOfTheForm(req, res);
processFormFieldsIndividual(req, res);
}
});
var port = process.env.PORT || 8080;
server.listen(port);
console.log("server listening on ", port);
// serve HTML file
// located according to the `FORM` env var
function displayForm(res) {
fs.readFile(process.env.FORM || 'form.html', function (err, data) {
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': data.length
});
res.write(data);
res.end();
});
}
// get the POST data
// and call the sendMail method
function processAllFieldsOfTheForm(req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
//Store the data from the fields in your data store.
//The data store could be a file or database or any other store based
//on your application.
res.writeHead(200, {
'content-type': 'text/plain'
});
res.write('received the data:\n\n');
res.end(util.inspect({
fields: fields,
files: files
}));
});
}
function processFormFieldsIndividual(req, res) {
//Store the data from the fields in your data store.
//The data store could be a file or database or any other store based
//on your application.
var fields = [];
var form = new formidable.IncomingForm();
form.on('field', function (field, value) {
console.log(field);
console.log(value);
fields[field] = value;
});
form.on('end', function () {
res.writeHead(200, {
'content-type': 'text/plain'
});
sendMail(util.inspect(fields));
res.write(process.env.MESSAGE || 'Thank you for your submission.');
res.end();
});
form.parse(req);
}
// setup the email sender
// uses the nodemailer lib
// sends the email to the adress found in the `TO` env var
let transporter = nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
secure: process.env.EMAIL_PORT === 465, // secure:true for port 465, secure:false for port 587
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS
}
});
function sendMail(text) {
// setup email data with unicode symbols
let mailOptions = {
from: process.env.FROM || 'Email form data bot <[email protected]>',
to: process.env.TO,
subject: 'New form submission' + (process.env.SITE_NAME ? ' on ' + process.env.SITE_NAME : ''),
text: text
};
console.log('sending email: ', mailOptions);
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
}