2019-02-07 00:52:48 +01:00
const tools = require ( './tools/general' ) ;
2018-12-24 20:31:02 +01:00
const Email = require ( 'email-templates' ) ;
var i18n = require ( "i18n" ) ;
const previewEmail = require ( 'preview-email' ) ;
2019-02-07 00:52:48 +01:00
const nodemailer = require ( "nodemailer" ) ;
2018-12-24 20:31:02 +01:00
const transport _preview = nodemailer . createTransport ( {
jsonTransport : true
} ) ;
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer . createTransport ( {
service : 'Gmail' ,
auth : {
user : process . env . EMAIL _FROM ,
pass : process . env . EMAIL _PW
}
} ) ;
function checkifSendEmail ( ) {
2018-12-27 21:03:42 +01:00
return process . env . SEND _EMAIL === "1" ;
//return false;
2018-12-24 20:31:02 +01:00
}
module . exports = {
sendEmail _base : function ( template , username , to , mylocalsconf ) {
console . log ( "check EMAIL :" + checkifSendEmail ( ) ) ;
const email = new Email ( {
message : {
from : process . env . EMAIL _FROM , // sender address
} ,
send : checkifSendEmail ( ) ,
preview : ! checkifSendEmail ( ) ,
transport : {
service : 'Gmail' ,
auth : {
user : process . env . EMAIL _FROM ,
pass : process . env . EMAIL _PW
}
} ,
} ) ;
email
. send ( {
template : template ,
message : {
to : to ,
} ,
locals : mylocalsconf ,
} )
. then ( console . log )
. catch ( console . error ) ;
} ,
2019-02-15 01:38:01 +01:00
sendEmail _Normale : function ( to , subject , html ) {
2018-12-24 20:31:02 +01:00
// setup e-mail data with unicode symbols
var mailOptions = {
from : process . env . EMAIL _FROM , // sender address
to : to ,
generateTextFromHTML : true ,
subject : subject ,
html : html ,
} ;
if ( process . env . SEND _EMAIL === 1 ) {
console . log ( "SEND EMAIL smtpTransport" ) ;
// send mail with defined transport object
smtpTransport . sendMail ( mailOptions , function ( error , response ) {
if ( error ) {
console . log ( error ) ;
} else {
console . log ( "Message sent: " + response ) ;
}
} ) ;
} else {
if ( process . env . PROVA _EMAIL _TEMPLATE !== "1" )
previewEmail ( mailOptions ) . then ( console . log ) . catch ( console . error ) ;
else
transport _preview . sendMail ( mailOptions ) . then ( console . log ) . catch ( console . error ) ;
}
} ,
getHostByIdApp : function ( idapp ) {
if ( idapp === 1 ) {
2018-12-27 21:03:42 +01:00
let siteport = ( process . env . PORT _APP1 !== "0" ) ? ( ':' + process . env . PORT _APP1 ) : "" ;
return process . env . URLBASE _APP1 + siteport ;
2018-12-24 20:31:02 +01:00
} else {
return ""
}
} ,
getNomeAppByIdApp : function ( idapp ) {
if ( idapp === 1 ) {
return process . env . NOME _APP1 ;
}
} ,
getlinkReg : function ( idapp , idreg ) {
2019-01-02 01:59:01 +01:00
strlinkreg = this . getHostByIdApp ( idapp ) + process . env . LINKVERIF _REG + ` ?idapp= ${ idapp } &idlink= ${ idreg } ` ;
2018-12-24 20:31:02 +01:00
return strlinkreg ;
} ,
getlinkRequestNewPassword : function ( idapp , user , tokenforgot ) {
strlinkreg = this . getHostByIdApp ( idapp ) + "/#" + process . env . LINK _REQUEST _NEWPASSWORD + ` ?idapp= ${ idapp } &username= ${ user } &=tokenforgot= ${ tokenforgot } ` ;
return strlinkreg ;
} ,
sendEmail _Registration : function ( lang , emailto , user , idapp , idreg ) {
mylocalsconf = {
locale : lang ,
nomeapp : this . getNomeAppByIdApp ( idapp ) ,
strlinkreg : this . getlinkReg ( idapp , idreg ) ,
user : user ,
forgetpwd : "" ,
emailto : emailto ,
} ;
this . sendEmail _base ( 'registration/' + lang , user , emailto , mylocalsconf ) ;
2019-02-15 01:38:01 +01:00
// Send to the Admin an Email
this . sendEmail _Normale ( process . env . EMAIL _FROM , 'Nuova Registrazione: (' + user + ")" , 'Effettuata una Nuova Registrazione!<br>Username:' + user + '<br></br>Email: ' + emailto + '<br>' )
2018-12-24 20:31:02 +01:00
} ,
sendEmail _RequestNewPassword : function ( lang , emailto , idapp , tokenforgot ) {
mylocalsconf = {
locale : lang ,
nomeapp : this . getNomeAppByIdApp ( idapp ) ,
user : user ,
strlinksetpassword : this . getlinkRequestNewPassword ( idapp , user , tokenforgot ) ,
emailto : emailto ,
} ;
this . sendEmail _base ( 'resetpwd/' + lang , user , emailto , mylocalsconf ) ;
} ,
} ;