48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
var nodemailer = require('nodemailer');
|
|
var settings = require('./Settings.js');
|
|
var logger = require('./Logger.js');
|
|
|
|
// 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) {
|
|
logger.print(__LOG_ERROR, error);
|
|
} else {
|
|
logger.print(__LOG_INFO, 'Conexión SMTP establecida');
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Envia un email con los datos recibidos desde un vector.
|
|
* @param {Object} data - Datos para el envío del email
|
|
*/
|
|
send: function(data) {
|
|
let mailOptions = {
|
|
from: '"' + settings.senderName + '" <' + settings.senderMail + '>',
|
|
to: data.recipient,
|
|
subject: data.subject,
|
|
html: data.body
|
|
};
|
|
|
|
this.transporter.sendMail(mailOptions, (error, info) => {
|
|
if (error) {
|
|
logger.print(__LOG_ERROR, error);
|
|
} else if (Settings.debug) {
|
|
logger.print(__LOG_INFO, 'Se ha enviado el email ' + info.messageId + ' [' + info.response + ']');
|
|
|
|
return true;
|
|
}
|
|
});
|
|
},
|
|
} |