Commit | Line | Data |
---|---|---|
8d7e2786 | 1 | const nodemailer = require('nodemailer'); |
0bd5933d | 2 | const params = require("../config/parameters"); |
8d7e2786 | 3 | |
0bd5933d | 4 | module.exports = function(from, to, subject, body, cb) |
8d7e2786 | 5 | { |
dac39588 BA |
6 | // Avoid the actual sending in development mode |
7 | if (params.env === 'development') | |
8 | { | |
9 | console.log("New mail: from " + from + " / to " + to); | |
10 | console.log("Subject: " + subject); | |
a749972c | 11 | console.log(body); |
fe4c7e67 | 12 | if (!cb) |
866842c3 BA |
13 | cb = (err) => { if (err) console.log(err); } |
14 | cb(); | |
15 | return; | |
dac39588 | 16 | } |
866842c3 BA |
17 | |
18 | // Production-only code from here: | |
19 | ||
20 | if (!cb) | |
fe4c7e67 | 21 | cb = () => {}; //default: do nothing (TODO: log somewhere) |
c018b304 | 22 | |
8d7e2786 | 23 | // Create reusable transporter object using the default SMTP transport |
dac39588 BA |
24 | const transporter = nodemailer.createTransport({ |
25 | host: params.mail.host, | |
26 | port: params.mail.port, | |
27 | secure: params.mail.secure, | |
28 | auth: { | |
29 | user: params.mail.user, | |
30 | pass: params.mail.pass | |
31 | } | |
32 | }); | |
8d7e2786 | 33 | |
dac39588 BA |
34 | // Setup email data with unicode symbols |
35 | const mailOptions = { | |
36 | from: params.mail.noreply, | |
37 | to: to, | |
38 | subject: subject, | |
39 | text: body, | |
58e7b94e | 40 | replyTo: from, |
8d7e2786 BA |
41 | }; |
42 | ||
dac39588 BA |
43 | // Send mail with the defined transport object |
44 | transporter.sendMail(mailOptions, (error, info) => { | |
8d7e2786 | 45 | // Ignore info. Option: |
dac39588 | 46 | //console.log('Message sent: %s', info.messageId); |
866842c3 | 47 | cb(error); |
8d7e2786 | 48 | }); |
0bd5933d | 49 | } |