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');
|
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) {
|
2017-06-07 13:28:42 +00:00
|
|
|
throw new Error(error);
|
|
|
|
} else if (settings.app().debug) {
|
|
|
|
console.log('SMTP connection stablished');
|
2017-05-30 06:06:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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-06-07 13:28:42 +00:00
|
|
|
send: function(recipient, subject, body, attachments, cb) {
|
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-06-07 13:28:42 +00:00
|
|
|
to: recipient,
|
|
|
|
subject: subject,
|
|
|
|
html: body,
|
|
|
|
attachments
|
2017-05-30 06:06:14 +00:00
|
|
|
};
|
2017-06-07 17:29:16 +00:00
|
|
|
|
2017-08-30 12:10:43 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production'){
|
|
|
|
mailOptions.to = settings.testEmail;
|
|
|
|
}
|
|
|
|
|
2017-05-30 06:06:14 +00:00
|
|
|
this.transporter.sendMail(mailOptions, (error, info) => {
|
|
|
|
if (error) {
|
2017-06-07 13:28:42 +00:00
|
|
|
return cb(null, 'Email not sent: ' + error);
|
2017-05-31 12:55:41 +00:00
|
|
|
} else if (settings.app().debug) {
|
2017-06-07 13:28:42 +00:00
|
|
|
console.log('Mail sent ' + info.messageId + ' [' + info.response + ']');
|
|
|
|
cb();
|
2017-05-30 06:06:14 +00:00
|
|
|
}
|
|
|
|
});
|
2017-05-31 16:19:55 +00:00
|
|
|
}
|
|
|
|
};
|