| 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 | console.log(body); |
| 12 | if (!cb) |
| 13 | cb = (err) => { if (!!err) console.log(err); } |
| 14 | return cb(); |
| 15 | } |
| 16 | else if (!cb) |
| 17 | cb = () => {}; //default: do nothing (TODO: log somewhere) |
| 18 | |
| 19 | // Create reusable transporter object using the default SMTP transport |
| 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 | }); |
| 29 | |
| 30 | // Setup email data with unicode symbols |
| 31 | const mailOptions = { |
| 32 | from: params.mail.noreply, |
| 33 | to: to, |
| 34 | subject: subject, |
| 35 | text: body, |
| 36 | replyTo: from, |
| 37 | }; |
| 38 | |
| 39 | // Send mail with the defined transport object |
| 40 | transporter.sendMail(mailOptions, (error, info) => { |
| 41 | if (!!error) |
| 42 | return cb(error); |
| 43 | // Ignore info. Option: |
| 44 | //console.log('Message sent: %s', info.messageId); |
| 45 | return cb(); |
| 46 | }); |
| 47 | } |