|
@@ -1,3 +1,9 @@
|
|
1
|
+"use strict"
|
|
2
|
+var pug = require('pug');
|
|
3
|
+var nodemailer = require('nodemailer');
|
|
4
|
+var settings = require('./settings');
|
|
5
|
+
|
|
6
|
+// Web server
|
1
|
7
|
var bodyParser = require('body-parser');
|
2
|
8
|
var express = require('express');
|
3
|
9
|
var app = express();
|
|
@@ -9,20 +15,89 @@ var log = printit({
|
9
|
15
|
date: true
|
10
|
16
|
});
|
11
|
17
|
|
|
18
|
+var transporter = nodemailer.createTransport(settings.mailserver);
|
12
|
19
|
|
13
|
20
|
// Serve static (JS + HTML) files
|
14
|
21
|
app.use(express.static('front'));
|
|
22
|
+// Body parsing
|
15
|
23
|
app.use(bodyParser.urlencoded({ extended: true }));
|
16
|
24
|
app.use(bodyParser.json());
|
17
|
25
|
|
18
|
|
-
|
|
26
|
+// A request on /send with user input = mail to be sent
|
19
|
27
|
app.post('/send', function(req, res, next) {
|
20
|
|
- res.header('Access-Control-Allow-Origin', '*');
|
21
|
|
- res.status(200).send();
|
22
|
|
- console.log(req.body);
|
|
28
|
+ // params will be used as:
|
|
29
|
+ // - values for html generation from the pug template
|
|
30
|
+ // - parameters for sending the mail(s)
|
|
31
|
+ let params = {
|
|
32
|
+ subject: req.body.subj,
|
|
33
|
+ from: req.body.name + ' <' + req.body.addr + '>',
|
|
34
|
+ html: req.body.text
|
|
35
|
+ };
|
|
36
|
+
|
|
37
|
+ // Replacing the mail's content with HTML from the pug template
|
|
38
|
+ // Commenting the line below will bypass the generation and only user the
|
|
39
|
+ // text entered by the user
|
|
40
|
+ params.html = pug.renderFile('template.pug', params);
|
|
41
|
+
|
|
42
|
+ log.info('Sending message from ' + content.from);
|
|
43
|
+
|
|
44
|
+ // Send the email to all users
|
|
45
|
+ sendMails(params, function(err, infos) {
|
|
46
|
+ if(err) {
|
|
47
|
+ log.error(err)
|
|
48
|
+ }
|
|
49
|
+ logStatus(infos);
|
|
50
|
+ }, function() {
|
|
51
|
+ res.header('Access-Control-Allow-Origin', '*');
|
|
52
|
+ res.status(200).send();
|
|
53
|
+ })
|
23
|
54
|
});
|
24
|
55
|
|
25
|
56
|
|
26
|
|
-app.listen(1970, function() {
|
27
|
|
- log.info("Server started");
|
|
57
|
+// Use either the default port or the one chosen by the user (PORT env variable)
|
|
58
|
+var port = process.env.PORT || 1970;
|
|
59
|
+// Start the server
|
|
60
|
+app.listen(port, function() {
|
|
61
|
+ log.info("Server started on port " + port);
|
28
|
62
|
});
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+// Send mails to the recipients specified in the JSON settings file
|
|
66
|
+// content: object containing mail params
|
|
67
|
+// {
|
|
68
|
+// subject: String
|
|
69
|
+// from: String (following RFC 1036 (https://tools.ietf.org/html/rfc1036#section-2.1.1))
|
|
70
|
+// html: String
|
|
71
|
+// }
|
|
72
|
+// update(next, infos): Called each time a mail is sent with the infos provided
|
|
73
|
+// by nodemailer
|
|
74
|
+// done(): Called once each mail has been sent
|
|
75
|
+function sendMails(params, update, done) {
|
|
76
|
+ let mails = settings.recipients.map((recipient) => {
|
|
77
|
+ return new Promise((sent) => {
|
|
78
|
+ params.to = recipient;
|
|
79
|
+
|
|
80
|
+ transporter.sendMail(params, (err, infos) => {
|
|
81
|
+ sent();
|
|
82
|
+ if(err) {
|
|
83
|
+ return next(err, recipient);
|
|
84
|
+ }
|
|
85
|
+ next(null, infos);
|
|
86
|
+ });
|
|
87
|
+ });
|
|
88
|
+ });
|
|
89
|
+ Promise.all(mails).then(done);
|
|
90
|
+}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+// Produces log from the infos provided by nodemailer
|
|
94
|
+// infos: infos provided by nodemailer
|
|
95
|
+// return: nothing
|
|
96
|
+function logStatus(infos) {
|
|
97
|
+ if(infos.accepted.length) {
|
|
98
|
+ log.info('Message sent to ' + status.accepted[0]);
|
|
99
|
+ }
|
|
100
|
+ if(infos.rejected.length) {
|
|
101
|
+ log.info('Message failed to send to ' + status.rejected[0]);
|
|
102
|
+ }
|
|
103
|
+}
|