salix/services/mailer/Application/mail.js

59 lines
2.4 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-30 06:06:14 +00:00
// 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());
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
*/
send: function(data) {
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: [
{filename: 'header.png', path: './Application/Template/images/header.png', cid: 'header'},
{filename: 'arrow.png', path: './Application/Template/images/arrow.png', cid: 'arrow'},
{filename: 'chat.png', path: './Application/Template/images/chat.png', cid: 'chat'},
{filename: 'facebook.png', path: './Application/Template/images/facebook.png', cid: 'facebook'},
{filename: 'twitter.png', path: './Application/Template/images/twitter.png', cid: 'twitter'},
{filename: 'youtube.png', path: './Application/Template/images/youtube.png', cid: 'youtube'},
{filename: 'pinterest.png', path: './Application/Template/images/pinterest.png', cid: 'pinterest'},
{filename: 'instagram.png', path: './Application/Template/images/instagram.png', cid: 'instagram'},
{filename: 'linkedin.png', path: './Application/Template/images/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);
} else if (settings.debug) {
2017-05-30 06:06:14 +00:00
logger.print(__LOG_INFO, 'Se ha enviado el email ' + info.messageId + ' [' + info.response + ']');
return true;
}
});
},
}