2017-05-30 06:06:14 +00:00
|
|
|
var nodemailer = require('nodemailer');
|
2017-05-31 11:46:18 +00:00
|
|
|
var settings = require('./settings.js');
|
|
|
|
var logger = require('./logger.js');
|
2017-05-31 12:55:41 +00:00
|
|
|
var path = require('path');
|
2017-05-30 06:06:14 +00:00
|
|
|
|
2017-05-31 16:19:55 +00:00
|
|
|
/**
|
|
|
|
* Módulo para el envío de emails
|
|
|
|
*/
|
2017-05-30 06:06:14 +00:00
|
|
|
module.exports = {
|
2017-05-30 06:06:14 +00:00
|
|
|
transporter: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Si todavía no está inicializada la configuración,
|
|
|
|
* carga el fichero de configuración.
|
|
|
|
*/
|
|
|
|
init: function() {
|
2017-05-31 11:46:18 +00:00
|
|
|
this.transporter = nodemailer.createTransport(settings.smtp());
|
2017-05-30 06:06:14 +00:00
|
|
|
|
|
|
|
this.transporter.verify(function(error, success) {
|
|
|
|
if (error) {
|
|
|
|
logger.print(__LOG_ERROR, error);
|
|
|
|
} else {
|
|
|
|
logger.print(__LOG_INFO, 'Conexión SMTP establecida');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-31 16:19:55 +00:00
|
|
|
/**
|
|
|
|
* Envia un email con los datos recibidos desde un vector.
|
|
|
|
* @param {Object} data - Datos para el envío del email
|
|
|
|
*/
|
2017-05-30 06:06:14 +00:00
|
|
|
send: function(data) {
|
2017-05-31 12:55:41 +00:00
|
|
|
var filePath = 'Template/images/';
|
2017-05-30 06:06:14 +00:00
|
|
|
let mailOptions = {
|
2017-05-31 11:46:18 +00:00
|
|
|
from: '"' + settings.app().senderName + '" <' + settings.app().senderMail + '>',
|
2017-05-30 06:06:14 +00:00
|
|
|
to: data.recipient,
|
|
|
|
subject: data.subject,
|
2017-05-31 11:46:18 +00:00
|
|
|
html: data.body,
|
|
|
|
attachments: [
|
2017-05-31 12:55:41 +00:00
|
|
|
{filename: 'header.png', path: path.join(__dirname, filePath, 'header.png'), cid: 'header'},
|
|
|
|
{filename: 'arrow.png', path: path.join(__dirname, filePath, 'arrow.png'), cid: 'arrow'},
|
|
|
|
{filename: 'chat.png', path: path.join(__dirname, filePath, 'chat.png'), cid: 'chat'},
|
|
|
|
{filename: 'facebook.png', path: path.join(__dirname, filePath, 'facebook.png'), cid: 'facebook'},
|
|
|
|
{filename: 'twitter.png', path: path.join(__dirname, filePath, 'twitter.png'), cid: 'twitter'},
|
|
|
|
{filename: 'youtube.png', path: path.join(__dirname, filePath, 'youtube.png'), cid: 'youtube'},
|
|
|
|
{filename: 'pinterest.png', path: path.join(__dirname, filePath, 'pinterest.png'), cid: 'pinterest'},
|
|
|
|
{filename: 'instagram.png', path: path.join(__dirname, filePath, 'instagram.png'), cid: 'instagram'},
|
|
|
|
{filename: 'linkedin.png', path: path.join(__dirname, filePath, 'linkedin.png'), cid: 'linkedin'}
|
2017-05-31 11:46:18 +00:00
|
|
|
]
|
2017-05-30 06:06:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.transporter.sendMail(mailOptions, (error, info) => {
|
|
|
|
if (error) {
|
|
|
|
logger.print(__LOG_ERROR, error);
|
2017-05-31 12:55:41 +00:00
|
|
|
} else if (settings.app().debug) {
|
2017-05-30 06:06:14 +00:00
|
|
|
logger.print(__LOG_INFO, 'Se ha enviado el email ' + info.messageId + ' [' + info.response + ']');
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2017-05-31 16:19:55 +00:00
|
|
|
}
|
|
|
|
};
|