136 lines
3.5 KiB
JavaScript
136 lines
3.5 KiB
JavaScript
var tools = require('./tools/general');
|
|
|
|
const Email = require('email-templates');
|
|
|
|
var i18n = require("i18n");
|
|
|
|
|
|
const previewEmail = require('preview-email');
|
|
var nodemailer = require("nodemailer");
|
|
|
|
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() {
|
|
//return process.env.SEND_EMAIL === "1";
|
|
return false;
|
|
}
|
|
|
|
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);
|
|
},
|
|
sendEmail_Normale: function (username, to, subject, html) {
|
|
|
|
// 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) {
|
|
return process.env.URLBASE_APP1 + ':' + process.env.PORT_APP1;
|
|
}else{
|
|
return ""
|
|
}
|
|
},
|
|
getNomeAppByIdApp: function (idapp) {
|
|
if (idapp === 1) {
|
|
return process.env.NOME_APP1;
|
|
}
|
|
},
|
|
getlinkReg: function (idapp, idreg) {
|
|
strlinkreg = this.getHostByIdApp(idapp) + "/#" + process.env.LINKVERIF_REG + `?idapp=${idapp}&idlink=${idreg}`;
|
|
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);
|
|
},
|
|
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);
|
|
},
|
|
|
|
};
|
|
|