Commit | Line | Data |
---|---|---|
e99c53fb BA |
1 | const params = require("../config/parameters"); |
2 | const { exec } = require('child_process'); | |
3 | ||
4 | let Mailer = | |
5 | { | |
6 | // o: {from(*), to(*), subject, body} - (*): mandatory | |
7 | send: function(o, callback) | |
8 | { | |
9 | let from = o.from; | |
10 | let to = o.to; | |
11 | let subject = !!o.subject ? o.subject : "[No subject]"; | |
12 | let body = !!o.body ? o.body : ""; | |
13 | ||
14 | // In development mode, just log message: | |
15 | let env = process.env.NODE_ENV || 'development'; | |
16 | if ('development' === env) | |
17 | { | |
18 | console.log("New mail: from " + from + " / to " + to); | |
19 | console.log("Subject: " + subject); | |
20 | let msgText = body.split('\\n'); | |
21 | msgText.forEach(msg => { console.log(msg); }); | |
22 | callback({}); | |
23 | } | |
24 | else | |
25 | { | |
26 | exec( | |
27 | "printf 'From: " + from + "\n" + | |
28 | "To: " + to + "\n" + | |
29 | "Subject: " + subject + "\n" + | |
30 | body + "' | msmtp -a " + params.mail.account + " " + to, | |
31 | (err, stdout, stderr) => { | |
32 | callback(err); | |
33 | // the *entire* stdout and stderr (buffered) | |
34 | //console.log("stdout: " + stdout); | |
35 | //console.log("stderr: " + stderr); | |
36 | } | |
37 | ); | |
38 | } | |
39 | } | |
40 | }; | |
41 | ||
42 | module.exports = Mailer; |