salix/services/mailer/Application/mail.js

61 lines
2.5 KiB
JavaScript

var nodemailer = require('nodemailer');
var settings = require('./settings.js');
var logger = require('./logger.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) {
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) {
var filePath = 'Template/images/';
let mailOptions = {
from: '"' + settings.app().senderName + '" <' + settings.app().senderMail + '>',
to: data.recipient,
subject: data.subject,
html: data.body,
attachments: [
{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'}
]
};
this.transporter.sendMail(mailOptions, (error, info) => {
if (error) {
logger.print(__LOG_ERROR, error);
} else if (settings.app().debug) {
logger.print(__LOG_INFO, 'Se ha enviado el email ' + info.messageId + ' [' + info.response + ']');
return true;
}
});
},
}