salix/services/mailer/application/mail.js

103 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-05-30 06:06:14 +00:00
var nodemailer = require('nodemailer');
var settings = require('./settings.js');
var template = require('./template.js');
var database = require('./database.js');
2017-05-30 06:06:14 +00:00
/**
* Mail module
*/
2017-05-30 06:06:14 +00:00
module.exports = {
2017-05-30 06:06:14 +00:00
transporter: null,
/**
* Load mail settings.
2017-05-30 06:06:14 +00:00
*/
init: function() {
this.transporter = nodemailer.createTransport(settings.smtp());
2017-05-30 06:06:14 +00:00
this.transporter.verify(function(error, success) {
if (error) {
2017-06-07 13:28:42 +00:00
throw new Error(error);
} else if (settings.app().debug) {
console.log('SMTP connection stablished');
2017-05-30 06:06:14 +00:00
}
});
},
/**
* Send email.
* @param {Object} recipient - Mail destinatary
* @param {String} subject - Subject
* @param {String} body - Mail body
* @param {Object} attachments - Mail attachments
* @param {Object} params - Params
* @param {Object} cb - Callback
*/
send: function(recipient, subject, body, attachments, params, cb) {
2017-05-30 06:06:14 +00:00
let mailOptions = {
from: '"' + settings.app().senderName + '" <' + settings.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
};
if (settings.app().debug) {
mailOptions.to = settings.testEmail;
}
2017-05-30 06:06:14 +00:00
this.transporter.sendMail(mailOptions, (error, info) => {
let status = (error ? error.message : 'OK');
this.log(params.sender, params.recipient, recipient, subject, body, params.message, status);
if (error)
return cb(new Error('Email not sent: ' + error));
if (settings.app().debug)
2017-06-07 13:28:42 +00:00
console.log('Mail sent ' + info.messageId + ' [' + info.response + ']');
cb();
});
},
/**
* Send email with template.
* @param {String} tplName - Template name
* @param {Object} params - Params object
* @param {Object} cb - Callback
*/
sendWithTemplate: function(tplName, params, cb) {
template.get(tplName, params, false, (error, result) => {
if (error)
return cb(error);
this.send(result.recipient, result.subject, result.body, result.attachments, params, error => {
if (error)
return cb(error);
cb();
});
2017-05-30 06:06:14 +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 (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
let qryParams = [senderId, recipientId, sender, settings.app().senderMail, subject, body, plainTextBody, 1, status];
database.pool.query(qry, qryParams, function(error, result) {
if (settings.app().debug && error)
console.log('Mail log: ' + error);
});
}
};