salix/services/mailer/application/mail.js

62 lines
2.4 KiB
JavaScript

var nodemailer = require('nodemailer');
var settings = require('./settings.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) {
console.log(error);
} else {
console.log('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) {
console.log(error);
} else if (settings.app().debug) {
console.log('Se ha enviado el email ' + info.messageId + ' [' + info.response + ']');
return true;
}
});
}
};