-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
105 lines (86 loc) · 2.65 KB
/
server.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
#!/usr/bin/env node
var express = require('express')
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var nconf = require('nconf');
var config = nconf
.argv()
.env()
.file({ file: 'config.json'})
.file('emails', { file: 'emails.json' });
var transport = nodemailer.createTransport(config.get('SMTP'));
var app = express();
// Needed for 'req.body'
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
app.post('/', function(req, res) {
// If the _gotcha field has a value, it's most likely a bot filling out
// the form. Fail silently.
if (req.body._gotcha) {
res.redirect(req.body._next || req.get('Referrer'));
return;
}
// Make sure the supplied key is valid
if (!config.get(req.query.key)) {
res.status(400).send({ error: 'Error invalid key' });
return;
}
// Construct Message
var emailMessage = req.body.message;
// Make sure the email message is not empty
if (!emailMessage) {
res.status(400).send({
error: 'Error, email message is empty'
});
return;
}
// Create string of _fields elements to add at end of message
if (emailMessage) {
emailMessage += '\r\n\r\n';
}
for(key in req.body) {
if (/^\_fields\.*/.test(key)) {
var value = req.body[key];
// Make the key into a pretty sentence
// (e.g. _fields.hiThere => Hi there)
var key = key
.replace('_fields.', '')
.replace(/^[a-z]|[A-Z]/g, function(v, i) {
return i === 0 ? v.toUpperCase() : " " + v.toLowerCase();
});
emailMessage += key + ': ' + value + '\r\n';
}
}
// Create an email message based on post values
var mailOpts = {
'text': emailMessage || 'No message was provided',
'subject': req.body._subject || 'Email from mailhound',
'from': {
'name': req.body.name,
'address': req.body._replyto || req.body.email
},
'to': config.get(req.query.key),
'cc': req.body._cc
}
// Send the message!
transport.sendMail(mailOpts, function(err, info) {
if (err) {
console.log('Error sending email: ' + err.name + ' - ' + err.message);
return res.status(500).send({ error: 'Error sending email' });
}
res.redirect(req.body._next || req.get('Referrer'));
});
});
var server = app.listen(config.get('PORT') || 8000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('mailhound listening at http://%s:%s', host, port);
});
process.on('SIGTERM', function() {
server.close(function() {
process.exit(0)
})
})