-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
98 lines (87 loc) · 2.88 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
const nodemailer = require('nodemailer');
const dotenv = require('dotenv');
const express = require('express');
const bodyParser = require('body-parser');
dotenv.config();
let fromEmail = process.env.FROM_EMAIL;
if (!fromEmail) {
console.log('Missing FROM_EMAIL env var');
process.exit(1);
}
let toEmail = process.env.TO_EMAIL;
if (!toEmail) {
console.log('Missing TO_EMAIL env var');
process.exit(1);
}
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT, 10) || 465,
secure: true, // secure:true for port 465, secure:false for port 587
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD
}
});
const app = express();
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', (req, res) => {
res.send('ready');
// res.send(
// '<form method=post action=/send>' +
// '<input type=text name=fromName placeholder=fromName value=Test>' +
// '<input type=text name=subject placeholder=subject>' +
// '<input type=text name=body placeholder=body>' +
// '<input type=text name=replyTo placeholder=replyTo>' +
// '<input type=submit value=Send>' +
// '</form>'
// );
});
app.post('/send', (req, res) => {
let fromName = req.body.fromName;
if (!fromName || typeof fromName !== 'string') {
res.status(400).send('Missing fromName');
return;
}
let subject = req.body.subject;
if (!subject || typeof subject !== 'string') {
res.status(400).send('Missing subject');
return;
}
let body = req.body.body;
if (!body || typeof body !== 'string') {
res.status(400).send('Missing body');
return;
}
let replyTo = req.body.replyTo;
if (replyTo && (typeof replyTo !== 'string' || !/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(replyTo))) {
res.status(400).send('Invalid replyTo');
return;
}
fromName = fromName.substring(0, 100).replace(/[^a-zA-Z0-9\-\.\_\ ]/, '');
subject = subject.substring(0, 200);
body = body.substring(0, parseInt(process.env.MAX_BODY_SIZE, 10) || 10000);
const mailOptions = {
from: '"' + fromName + '" <' + fromEmail + '>',
to: toEmail,
subject: subject,
text: body,
};
if (replyTo) {
mailOptions.replyTo = replyTo;
}
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.log(err);
res.status(500).send(err.message);
return;
}
console.log('Message %s sent: %s', info.messageId, info.response);
res.send('sent');
});
});
const server = app.listen(app.get('port'), () => {
const port = server.address().port;
console.log('Server running at http://localhost:' + port);
});