Separate client and server codes. Keep everything in one git repo for simplicity
[vchess.git] / server / 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 // 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 return cb();
14 }
15
16 // Create reusable transporter object using the default SMTP transport
17 const transporter = nodemailer.createTransport({
18 host: params.mail.host,
19 port: params.mail.port,
20 secure: params.mail.secure,
21 auth: {
22 user: params.mail.user,
23 pass: params.mail.pass
24 }
25 });
26
27 // Setup email data with unicode symbols
28 const mailOptions = {
29 from: from, //note: some SMTP serves might forbid this
30 to: to,
31 subject: subject,
32 text: body,
33 };
34
35 // Send mail with the defined transport object
36 transporter.sendMail(mailOptions, (error, info) => {
37 if (!!error)
38 return cb(error);
39 // Ignore info. Option:
40 //console.log('Message sent: %s', info.messageId);
41 return cb();
42 });
43 }