salix/services/mailer/application/mail.js

50 lines
1.4 KiB
JavaScript

var nodemailer = require('nodemailer');
var settings = require('./settings.js');
var path = require('path');
/**
* Módulo para el envío de emails
*/
module.exports = {
transporter: null,
/**
* Si todavía no está inicializada la configuración,
* carga el fichero de configuración.
*/
init: function() {
this.transporter = nodemailer.createTransport(settings.smtp());
this.transporter.verify(function(error, success) {
if (error) {
throw new Error(error);
} else if (settings.app().debug) {
console.log('SMTP connection stablished');
}
});
},
/**
* Envia un email con los datos recibidos desde un vector.
* @param {Object} data - Datos para el envío del email
*/
send: function(recipient, subject, body, attachments, cb) {
let mailOptions = {
from: '"' + settings.app().senderName + '" <' + settings.app().senderMail + '>',
to: recipient,
subject: subject,
html: body,
attachments
};
this.transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return cb(null, 'Email not sent: ' + error);
} else if (settings.app().debug) {
console.log('Mail sent ' + info.messageId + ' [' + info.response + ']');
cb();
}
});
}
};