| 1 | const nodemailer = require('nodemailer'); |
| 2 | const params = require("../config/parameters"); |
| 3 | |
| 4 | module.exports = function(from, to, subject, body, cb) |
| 5 | { |
| 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); |
| 11 | let msgText = body.split('\\n'); |
| 12 | msgText.forEach(msg => { console.log(msg); }); |
| 13 | if (!cb) |
| 14 | cb = (err) => { if (!!err) console.log(err); } |
| 15 | return cb(); |
| 16 | } |
| 17 | else if (!cb) |
| 18 | cb = () => {}; //default: do nothing (TODO: log somewhere) |
| 19 | |
| 20 | // Create reusable transporter object using the default SMTP transport |
| 21 | const transporter = nodemailer.createTransport({ |
| 22 | host: params.mail.host, |
| 23 | port: params.mail.port, |
| 24 | secure: params.mail.secure, |
| 25 | auth: { |
| 26 | user: params.mail.user, |
| 27 | pass: params.mail.pass |
| 28 | } |
| 29 | }); |
| 30 | |
| 31 | // Setup email data with unicode symbols |
| 32 | const mailOptions = { |
| 33 | from: params.mail.noreply, |
| 34 | to: to, |
| 35 | subject: subject, |
| 36 | text: body, |
| 37 | replyTo: from, |
| 38 | }; |
| 39 | |
| 40 | // Send mail with the defined transport object |
| 41 | transporter.sendMail(mailOptions, (error, info) => { |
| 42 | if (!!error) |
| 43 | return cb(error); |
| 44 | // Ignore info. Option: |
| 45 | //console.log('Message sent: %s', info.messageId); |
| 46 | return cb(); |
| 47 | }); |
| 48 | } |