Browse Source

Worked on the server

Brendan Abolivier 8 years ago
parent
commit
bf163688e1
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
5 changed files with 101 additions and 9 deletions
  1. 1
    1
      front/form.js
  2. 2
    1
      package.json
  3. 81
    6
      server.js
  4. 16
    0
      settings.example.json
  5. 1
    1
      template.pug

+ 1
- 1
front/form.js View File

@@ -9,7 +9,7 @@ var server  = getServer();
9 9
 var xhrSend = new XMLHttpRequest();
10 10
 xhrSend.onreadystatechange = function() {
11 11
     if (xhrSend.readyState == XMLHttpRequest.DONE) {
12
-        console.log(xhrSend);
12
+        console.log(xhrSend.response);
13 13
     }
14 14
 };
15 15
 

+ 2
- 1
package.json View File

@@ -19,7 +19,8 @@
19 19
     "express": "4.14.0",
20 20
     "node-minify": "1.3.9",
21 21
     "nodemailer": "2.4.2",
22
-    "printit": "0.1.20"
22
+    "printit": "0.1.20",
23
+    "pug": "2.0.0-beta6"
23 24
   },
24 25
   "license": {
25 26
     "type": "GPL-3.0",

+ 81
- 6
server.js View File

@@ -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
+}

+ 16
- 0
settings.example.json View File

@@ -0,0 +1,16 @@
1
+{
2
+    "mailserver": {
3
+        "pool": true,
4
+        "host": "mail.example.tld",
5
+        "port": 465,
6
+        "secure": true,
7
+        "auth": {
8
+            "user": "noreply@noreply.tld",
9
+            "pass": "hackme"
10
+        }
11
+    },
12
+    "recipients": [
13
+        "you@example.tld",
14
+        "someone.else@example.com"
15
+    ]
16
+}

+ 1
- 1
template.pug View File

@@ -7,4 +7,4 @@ html
7 7
             span(style="font-weight:bold") Sent from:&nbsp;
8 8
             span= from
9 9
         
10
-        p= text
10
+        p= html