2017-05-30 06:06:14 +00:00
|
|
|
var nodemailer = require('nodemailer');
|
2017-11-27 14:08:18 +00:00
|
|
|
var config = require('./config.js');
|
2017-08-30 12:50:46 +00:00
|
|
|
var template = require('./template.js');
|
2017-08-31 14:11:37 +00:00
|
|
|
var database = require('./database.js');
|
2017-05-30 06:06:14 +00:00
|
|
|
|
2017-05-31 16:19:55 +00:00
|
|
|
/**
|
2017-08-30 12:50:46 +00:00
|
|
|
* Mail module
|
2017-05-31 16:19:55 +00:00
|
|
|
*/
|
2017-05-30 06:06:14 +00:00
|
|
|
module.exports = {
|
2017-05-30 06:06:14 +00:00
|
|
|
transporter: null,
|
|
|
|
/**
|
2017-11-27 14:08:18 +00:00
|
|
|
* Load mail config.
|
2017-05-30 06:06:14 +00:00
|
|
|
*/
|
|
|
|
init: function() {
|
2018-01-03 12:41:22 +00:00
|
|
|
if (process.env.NODE_ENV == 'test')
|
|
|
|
return;
|
2017-12-27 08:44:27 +00:00
|
|
|
|
2017-11-27 14:08:18 +00:00
|
|
|
this.transporter = nodemailer.createTransport(config.smtp);
|
2017-05-30 06:06:14 +00:00
|
|
|
|
|
|
|
this.transporter.verify(function(error, success) {
|
|
|
|
if (error) {
|
2017-12-27 08:44:27 +00:00
|
|
|
console.error(error);
|
2017-11-27 14:08:18 +00:00
|
|
|
} else if (config.app.debug) {
|
2017-06-07 13:28:42 +00:00
|
|
|
console.log('SMTP connection stablished');
|
2017-05-30 06:06:14 +00:00
|
|
|
}
|
|
|
|
});
|
2017-12-27 08:44:27 +00:00
|
|
|
|
2017-05-30 06:06:14 +00:00
|
|
|
},
|
|
|
|
|
2017-05-31 16:19:55 +00:00
|
|
|
/**
|
2017-08-30 12:50:46 +00:00
|
|
|
* Send email.
|
|
|
|
* @param {Object} recipient - Mail destinatary
|
|
|
|
* @param {String} subject - Subject
|
|
|
|
* @param {String} body - Mail body
|
|
|
|
* @param {Object} attachments - Mail attachments
|
2017-08-31 14:11:37 +00:00
|
|
|
* @param {Object} params - Params
|
2017-08-30 12:50:46 +00:00
|
|
|
* @param {Object} cb - Callback
|
2017-05-31 16:19:55 +00:00
|
|
|
*/
|
2017-08-31 14:11:37 +00:00
|
|
|
send: function(recipient, subject, body, attachments, params, cb) {
|
2018-01-03 12:41:22 +00:00
|
|
|
if (process.env.NODE_ENV == 'test')
|
|
|
|
return;
|
|
|
|
|
2017-05-30 06:06:14 +00:00
|
|
|
let mailOptions = {
|
2017-11-27 14:08:18 +00:00
|
|
|
from: '"' + config.app.senderName + '" <' + config.app.senderMail + '>',
|
2017-06-07 13:28:42 +00:00
|
|
|
to: recipient,
|
|
|
|
subject: subject,
|
|
|
|
html: body,
|
|
|
|
attachments
|
2017-05-30 06:06:14 +00:00
|
|
|
};
|
2017-06-07 17:29:16 +00:00
|
|
|
|
2018-01-03 12:41:22 +00:00
|
|
|
if (process.env.NODE_ENV != 'production')
|
2017-11-27 14:08:18 +00:00
|
|
|
mailOptions.to = config.smtp.testEmail;
|
2017-08-30 12:10:43 +00:00
|
|
|
|
2017-05-30 06:06:14 +00:00
|
|
|
this.transporter.sendMail(mailOptions, (error, info) => {
|
2017-10-24 22:38:02 +00:00
|
|
|
try {
|
|
|
|
let status = (error ? error.message : 'OK');
|
|
|
|
this.log(params.sender, params.recipient, recipient, subject, body, params.message, status);
|
2017-08-31 14:11:37 +00:00
|
|
|
|
2017-10-24 22:38:02 +00:00
|
|
|
if (error)
|
|
|
|
return cb(new Error('Email not sent: ' + error));
|
2017-08-31 14:11:37 +00:00
|
|
|
|
2017-11-27 14:08:18 +00:00
|
|
|
if (config.app.debug)
|
2017-10-24 22:38:02 +00:00
|
|
|
console.log('Mail sent ' + info.messageId + ' [' + info.response + ']');
|
2017-08-31 14:11:37 +00:00
|
|
|
|
2017-10-24 22:38:02 +00:00
|
|
|
cb();
|
2018-01-16 20:39:40 +00:00
|
|
|
} catch (e) {
|
2017-10-24 22:38:02 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2017-08-30 12:50:46 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send email with template.
|
|
|
|
* @param {String} tplName - Template name
|
|
|
|
* @param {Object} params - Params object
|
|
|
|
* @param {Object} cb - Callback
|
|
|
|
*/
|
|
|
|
sendWithTemplate: function(tplName, params, cb) {
|
2017-11-27 14:08:18 +00:00
|
|
|
template.get(tplName, params, (error, result) => {
|
2017-10-17 06:22:59 +00:00
|
|
|
if (error)
|
|
|
|
return cb(error);
|
|
|
|
|
2017-11-27 14:08:18 +00:00
|
|
|
// Custom attachments
|
|
|
|
if (params.attachments)
|
|
|
|
params.attachments.forEach(function(attachment) {
|
|
|
|
result.attachments.push(attachment);
|
|
|
|
});
|
2018-01-16 20:39:40 +00:00
|
|
|
|
2017-10-17 06:22:59 +00:00
|
|
|
this.send(result.recipient, result.subject, result.body, result.attachments, params, error => {
|
|
|
|
if (error)
|
|
|
|
return cb(error);
|
2017-08-31 14:11:37 +00:00
|
|
|
|
2017-10-17 06:22:59 +00:00
|
|
|
cb();
|
2017-08-30 12:50:46 +00:00
|
|
|
});
|
2017-05-30 06:06:14 +00:00
|
|
|
});
|
2017-08-31 14:11:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add mail log
|
|
|
|
* @param {Integer} senderId - Sender id
|
|
|
|
* @param {Integer} recipientId - Recipient id
|
|
|
|
* @param {String} sender - Sender email
|
|
|
|
* @param {String} subject - Mail subject
|
|
|
|
* @param {String} body - Mail body
|
|
|
|
* @param {String} plainTextBody - Mail plain text body
|
|
|
|
* @param {String} status - Mail status
|
|
|
|
*/
|
|
|
|
log: function(senderId, recipientId, sender, subject, body, plainTextBody, status) {
|
|
|
|
let qry = `INSERT INTO mail(senderFk, recipientFk, sender, replyTo, subject, body, plainTextBody, sent, status)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
|
2017-11-27 14:08:18 +00:00
|
|
|
let qryParams = [senderId, recipientId, sender, config.app.senderMail, subject, body, plainTextBody, 1, status];
|
2017-08-31 14:11:37 +00:00
|
|
|
|
|
|
|
database.pool.query(qry, qryParams, function(error, result) {
|
2017-11-27 14:08:18 +00:00
|
|
|
if (config.app.debug && error)
|
2017-08-31 14:11:37 +00:00
|
|
|
console.log('Mail log: ' + error);
|
|
|
|
});
|
2017-05-31 16:19:55 +00:00
|
|
|
}
|
|
|
|
};
|