b0a0bacef90f69ac18860f409c58342cdbab1f7b
[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 console.log(body);
12 if (!cb)
13 cb = (err) => { if (err) console.log(err); }
14 cb();
15 return;
16 }
17
18 // Production-only code from here:
19
20 if (!cb)
21 cb = () => {}; //default: do nothing (TODO: log somewhere)
22
23 // Create reusable transporter object using the default SMTP transport
24 const transporter = nodemailer.createTransport({
25 host: params.mail.host,
26 port: params.mail.port,
27 secure: params.mail.secure,
28 auth: {
29 user: params.mail.user,
30 pass: params.mail.pass
31 }
32 });
33
34 // Setup email data with unicode symbols
35 const mailOptions = {
36 from: params.mail.noreply,
37 to: to,
38 subject: subject,
39 text: body,
40 replyTo: from,
41 };
42
43 // Send mail with the defined transport object
44 transporter.sendMail(mailOptions, (error, info) => {
45 // Ignore info. Option:
46 //console.log('Message sent: %s', info.messageId);
47 cb(error);
48 });
49 }