Sanitize inputs on server side
[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 = {
58e7b94e 29 from: params.mail.noreply,
8d7e2786
BA
30 to: to,
31 subject: subject,
32 text: body,
58e7b94e 33 replyTo: from,
8d7e2786
BA
34 };
35
8d7e2786
BA
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 });
0bd5933d 44}