Roughly completed Users logic; untested
[vchess.git] / utils / mailer.js
1 const nodemailer = require('nodemailer');
2 const params = require("../config/parameters");
3
4 module.exports = function(from, to, subject, body, cb)
5 {
6 // Create reusable transporter object using the default SMTP transport
7 const transporter = nodemailer.createTransport({
8 host: params.mail.host,
9 port: params.mail.port,
10 secure: params.mail.secure,
11 auth: {
12 user: params.mail.user,
13 pass: params.mail.pass
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 });
44 }