Separate client and server codes. Keep everything in one git repo for simplicity
[vchess.git] / server / utils / mailer.js
CommitLineData
8d7e2786 1const nodemailer = require('nodemailer');
0bd5933d 2const params = require("../config/parameters");
8d7e2786 3
0bd5933d 4module.exports = function(from, to, subject, body, cb)
8d7e2786 5{
c018b304
BA
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
8d7e2786
BA
16 // Create reusable transporter object using the default SMTP transport
17 const transporter = nodemailer.createTransport({
0bd5933d
BA
18 host: params.mail.host,
19 port: params.mail.port,
20 secure: params.mail.secure,
8d7e2786 21 auth: {
0bd5933d
BA
22 user: params.mail.user,
23 pass: params.mail.pass
8d7e2786
BA
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
8d7e2786
BA
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 });
0bd5933d 43}