| 1 | const nodemailer = require('nodemailer'); |
| 2 | const params = require("../config/parameters"); |
| 3 | |
| 4 | module.exports = function(from, to, subject, body, cb) { |
| 5 | // Avoid the actual sending in development mode |
| 6 | if (params.env === 'development') { |
| 7 | console.log("New mail: from " + from + " / to " + to); |
| 8 | console.log("Subject: " + subject); |
| 9 | console.log(body); |
| 10 | if (!cb) cb = (err) => { if (err) console.log(err); } |
| 11 | cb(); |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | // Production-only code from here: |
| 16 | |
| 17 | // Default: do nothing (TODO: log somewhere) |
| 18 | if (!cb) cb = () => {}; |
| 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 | // Ignore info. Option: |
| 43 | //console.log('Message sent: %s', info.messageId); |
| 44 | cb(error); |
| 45 | }); |
| 46 | }; |