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 BA |
12 | if (!cb) |
13 | cb = (err) => { if (!!err) console.log(err); } | |
dac39588 BA |
14 | return cb(); |
15 | } | |
fe4c7e67 BA |
16 | else if (!cb) |
17 | cb = () => {}; //default: do nothing (TODO: log somewhere) | |
c018b304 | 18 | |
8d7e2786 | 19 | // Create reusable transporter object using the default SMTP transport |
dac39588 BA |
20 | const transporter = nodemailer.createTransport({ |
21 | host: params.mail.host, | |
22 | port: params.mail.port, | |
23 | secure: params.mail.secure, | |
24 | auth: { | |
25 | user: params.mail.user, | |
26 | pass: params.mail.pass | |
27 | } | |
28 | }); | |
8d7e2786 | 29 | |
dac39588 BA |
30 | // Setup email data with unicode symbols |
31 | const mailOptions = { | |
32 | from: params.mail.noreply, | |
33 | to: to, | |
34 | subject: subject, | |
35 | text: body, | |
58e7b94e | 36 | replyTo: from, |
8d7e2786 BA |
37 | }; |
38 | ||
dac39588 BA |
39 | // Send mail with the defined transport object |
40 | transporter.sendMail(mailOptions, (error, info) => { | |
41 | if (!!error) | |
42 | return cb(error); | |
8d7e2786 | 43 | // Ignore info. Option: |
dac39588 BA |
44 | //console.log('Message sent: %s', info.messageId); |
45 | return cb(); | |
8d7e2786 | 46 | }); |
0bd5933d | 47 | } |