/*made by Miguel Rodrigues @ KBZ miguel.rodrigues@knowledgebiz.pt*/
//Emailer to to send Notification by Email
var email = require('nodemailer');
var emailconfig = require('../config/email');
var emailbody = require('../signature');
var logger = require('../config/logger.js');
var handler = "backend.controllers.emailer";
var transporter= email.createTransport({
service: "gmail",
auth: {
//type: 'OAuth2',
user: emailconfig.userEmail,
pass: emailconfig.userPwd
}
});
/*
description: Sends an email using the email setup
input:
emailTo: String Array of email destinations ex: ["destination1@email.com","destination2@email.com"]
notification: String with the email body
*/
module.exports.sendEmail = function (emailTo,notification) {
//var destinations = "'";
/*
var destinations ="";
for(i=0;i<emailTo.length;i++){
destinations += emailTo[i];
if(i<emailTo.length-1 && emailTo.length != 1){
destinations += ", "
}
}
*/
/*
if(emailTo.length != 1){
destinations += "'";
}
*/
var mailOptions={
from: emailconfig.userEmail,
//bcc: destinations,
bcc: emailTo,
subject: 'New notification!',
html: '<h4>'+notification+'</h4>'+emailbody.signature
};
logger.debug("sendEmail","Recipients",mailOptions);
transporter.sendMail(mailOptions, function(error,info){
if(error){
logger.error(handler, error);
} else {
logger.info(handler, info.response);
}
});
}
module.exports.sendEmailToken = function (emailTo,token) {
var mailOptions={
from: emailconfig.userEmail,
to: emailTo,
subject: 'New Token!',
html: '<h4>vApp Token for Notification Enabler: <br><br>'+token+'</h4>'+emailbody.signature
};
transporter.sendMail(mailOptions, function(error,info){
if(error){
logger.error(handler, error);
//console.log(error);
} else {
logger.info(handler, info.response);
//console.log('Email sent: '+ info.response);
}
});
}
module.exports.sendEmailNewRule = function (emailTo,newRule) {
var mailOptions={
from: emailconfig.userEmail,
to: emailTo,
subject: 'Intelligent creation of a new Rule!',
html: '<h4>'+newRule+'<br><br></h4>'+emailbody.signature
};
transporter.sendMail(mailOptions, function(error,info){
if(error){
logger.error(handler, error);
} else {
logger.info(handler, info.response);
}
});
}