salix/services/mailer/Application/mail.js

63 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-05-30 06:06:14 +00:00
var nodemailer = require('nodemailer');
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
/**
* 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() {
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');
}
});
},
/**
* 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 = {
from: '"' + settings.app().senderName + '" <' + settings.app().senderMail + '>',
2017-05-30 06:06:14 +00:00
to: data.recipient,
subject: data.subject,
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-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;
}
});
}
};