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 BA |
5 | { |
6 | // Create reusable transporter object using the default SMTP transport | |
7 | const transporter = nodemailer.createTransport({ | |
0bd5933d BA |
8 | host: params.mail.host, |
9 | port: params.mail.port, | |
10 | secure: params.mail.secure, | |
8d7e2786 | 11 | auth: { |
0bd5933d BA |
12 | user: params.mail.user, |
13 | pass: params.mail.pass | |
8d7e2786 BA |
14 | } |
15 | }); | |
16 | ||
17 | // Setup email data with unicode symbols | |
18 | const mailOptions = { | |
19 | from: from, //note: some SMTP serves might forbid this | |
20 | to: to, | |
21 | subject: subject, | |
22 | text: body, | |
23 | }; | |
24 | ||
25 | // Avoid the actual sending in development mode | |
26 | const env = process.env.NODE_ENV || 'development'; | |
27 | if ('development' === env) | |
28 | { | |
29 | console.log("New mail: from " + from + " / to " + to); | |
30 | console.log("Subject: " + subject); | |
31 | let msgText = body.split('\\n'); | |
32 | msgText.forEach(msg => { console.log(msg); }); | |
33 | return cb(); | |
34 | } | |
35 | ||
36 | // Send mail with the defined transport object | |
37 | transporter.sendMail(mailOptions, (error, info) => { | |
38 | if (!!error) | |
39 | return cb(error); | |
40 | // Ignore info. Option: | |
41 | //console.log('Message sent: %s', info.messageId); | |
42 | return cb(); | |
43 | }); | |
0bd5933d | 44 | } |