- Import emails from a list to a DB
- Create Template Emails - Options Email
This commit is contained in:
@@ -8,6 +8,8 @@ var i18n = require("i18n");
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
const { Settings } = require('./models/settings');
|
||||
const { TemplEmail } = require('./models/templemail');
|
||||
const { Discipline } = require('./models/discipline');
|
||||
|
||||
const previewEmail = require('preview-email');
|
||||
const nodemailer = require("nodemailer");
|
||||
@@ -16,19 +18,12 @@ const { MyEvent } = require('./models/myevent');
|
||||
const { MailingList } = require('./models/mailinglist');
|
||||
const { Newstosent } = require('./models/newstosent');
|
||||
|
||||
const server_constants = require('./tools/server_constants');
|
||||
|
||||
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;
|
||||
@@ -36,30 +31,44 @@ function checkifSendEmail() {
|
||||
|
||||
|
||||
module.exports = {
|
||||
sendEmail_base: function (template, to, mylocalsconf, replyTo) {
|
||||
sendEmail_base: function (template, to, mylocalsconf, replyTo, transport, previewonly) {
|
||||
|
||||
console.log('mylocalsconf', mylocalsconf);
|
||||
// console.log('mylocalsconf', mylocalsconf);
|
||||
|
||||
// console.log("check EMAIL :" + checkifSendEmail());
|
||||
|
||||
const email = new Email({
|
||||
const paramemail = {
|
||||
message: {
|
||||
from: process.env.EMAIL_FROM, // sender address
|
||||
from: mylocalsconf.dataemail.from, // sender address
|
||||
headers: {
|
||||
'Reply-To': replyTo
|
||||
}
|
||||
},
|
||||
send: checkifSendEmail(),
|
||||
preview: !checkifSendEmail(),
|
||||
transport: {
|
||||
service: 'Gmail',
|
||||
};
|
||||
|
||||
if (previewonly === undefined) {
|
||||
paramemail.preview = !checkifSendEmail();
|
||||
paramemail.send = checkifSendEmail();
|
||||
} else {
|
||||
paramemail.preview = previewonly;
|
||||
paramemail.send = !previewonly;
|
||||
}
|
||||
|
||||
if (transport) {
|
||||
paramemail.transport = transport;
|
||||
} else {
|
||||
paramemail.transport = {
|
||||
service: 'gmail',
|
||||
auth: {
|
||||
user: process.env.EMAIL_FROM,
|
||||
pass: process.env.EMAIL_PW
|
||||
}
|
||||
},
|
||||
// htmlToText: false
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
console.log('paramemail', paramemail);
|
||||
|
||||
const email = new Email(paramemail);
|
||||
|
||||
return email
|
||||
.send({
|
||||
@@ -126,11 +135,12 @@ module.exports = {
|
||||
strlinkreg = tools.getHostByIdApp(idapp) + "/#" + process.env.LINK_REQUEST_NEWPASSWORD + `?idapp=${idapp}&username=${user}&=tokenforgot=${tokenforgot}`;
|
||||
return strlinkreg;
|
||||
},
|
||||
sendEmail_Registration: function (lang, emailto, user, idapp, idreg) {
|
||||
sendEmail_Registration: async function (lang, emailto, user, idapp, idreg) {
|
||||
|
||||
console.log('idapp', idapp, tools.getNomeAppByIdApp(idapp));
|
||||
|
||||
mylocalsconf = {
|
||||
dataemail: await this.getdataemail(idapp),
|
||||
locale: lang,
|
||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||
strlinkreg: this.getlinkReg(idapp, idreg),
|
||||
@@ -150,9 +160,10 @@ module.exports = {
|
||||
this.sendEmail_base('admin/registration/' + lang, tools.getManagerEmailByIdApp(idapp), mylocalsconf, '');
|
||||
}
|
||||
},
|
||||
sendEmail_RequestNewPassword: function (lang, emailto, idapp, tokenforgot) {
|
||||
sendEmail_RequestNewPassword: async function (lang, emailto, idapp, tokenforgot) {
|
||||
|
||||
mylocalsconf = {
|
||||
dataemail: await this.getdataemail(idapp),
|
||||
locale: lang,
|
||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||
user: user,
|
||||
@@ -163,12 +174,13 @@ module.exports = {
|
||||
this.sendEmail_base('resetpwd/' + lang, emailto, mylocalsconf, '');
|
||||
},
|
||||
|
||||
sendEmail_Booking: function (res, lang, emailto, user, idapp, recbooking) {
|
||||
sendEmail_Booking: async function (res, lang, emailto, user, idapp, recbooking) {
|
||||
|
||||
tools.mylog('sendEmail_Booking');
|
||||
tools.mylog('tools.getNomeAppByIdApp(idapp)', tools.getNomeAppByIdApp(idapp), idapp);
|
||||
|
||||
mylocalsconf = {
|
||||
dataemail: await this.getdataemail(idapp),
|
||||
locale: lang,
|
||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||
name: user.name,
|
||||
@@ -206,11 +218,12 @@ module.exports = {
|
||||
|
||||
},
|
||||
|
||||
sendEmail_CancelBooking: function (res, lang, emailto, user, idapp, recbooking) {
|
||||
sendEmail_CancelBooking: async function (res, lang, emailto, user, idapp, recbooking) {
|
||||
|
||||
tools.mylog('sendEmail_CancelBooking');
|
||||
|
||||
mylocalsconf = {
|
||||
dataemail: await this.getdataemail(idapp),
|
||||
locale: lang,
|
||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||
name: user.name,
|
||||
@@ -234,12 +247,13 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
sendEmail_Msg: function (res, lang, emailto, user, idapp, recmsg) {
|
||||
sendEmail_Msg: async function (res, lang, emailto, user, idapp, recmsg) {
|
||||
|
||||
tools.mylog('sendEmail_Msg');
|
||||
tools.mylog('tools.getNomeAppByIdApp(idapp)', tools.getNomeAppByIdApp(idapp), idapp);
|
||||
|
||||
mylocalsconf = {
|
||||
dataemail: await this.getdataemail(idapp),
|
||||
locale: lang,
|
||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||
name: user.name,
|
||||
@@ -262,35 +276,184 @@ module.exports = {
|
||||
// Send Email also to the Admin
|
||||
// this.sendEmail_base('admin/sendmsg/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf);
|
||||
},
|
||||
sendEmail_Notif_Added_to_Newsletter: function (lang, user, idapp) {
|
||||
Add_to_MailingList_AndSendEmailNotify: async function (lang, user, idapp, sendnews) {
|
||||
|
||||
console.log('idapp', idapp, tools.getNomeAppByIdApp(idapp));
|
||||
|
||||
mylocalsconf = {
|
||||
dataemail: await this.getdataemail(idapp),
|
||||
locale: lang,
|
||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||
name: user.name,
|
||||
surname: user.surname,
|
||||
emailto: user.email,
|
||||
idapp
|
||||
};
|
||||
|
||||
// Add to the database
|
||||
const hash = tools.getHash(mylocalsconf.emailto);
|
||||
|
||||
myperson = new MailingList({ name: mylocalsconf.name, surname: mylocalsconf.surname, email: mylocalsconf.emailto });
|
||||
myperson._id = new ObjectID();
|
||||
myperson.idapp = idapp;
|
||||
|
||||
// Add new record to the DB MailingList
|
||||
return myperson.save().then((res) => {
|
||||
|
||||
// Send to the Admin an Email
|
||||
this.sendEmail_base('admin/added_to_newsletter/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '');
|
||||
|
||||
if (tools.isManagAndAdminDifferent(idapp)) {
|
||||
this.sendEmail_base('admin/added_to_newsletter/' + lang, tools.getManagerEmailByIdApp(idapp), mylocalsconf, '');
|
||||
// Check if exist to the Mailing List
|
||||
let myperson = await MailingList.findByHash(idapp, hash);
|
||||
if (!myperson || !myperson.statesub) {
|
||||
if (!myperson) {
|
||||
myperson = new MailingList({
|
||||
name: mylocalsconf.name,
|
||||
surname: mylocalsconf.surname,
|
||||
email: mylocalsconf.emailto,
|
||||
hash
|
||||
});
|
||||
myperson._id = new ObjectID();
|
||||
} else {
|
||||
myperson.name = mylocalsconf.name;
|
||||
myperson.surname = mylocalsconf.surname;
|
||||
//myperson.email = mylocalsconf.emailto;
|
||||
//myperson.hash = hash;
|
||||
}
|
||||
|
||||
});
|
||||
myperson.idapp = idapp;
|
||||
myperson.statesub = true; // subscription
|
||||
|
||||
// Add/save new record to the DB MailingList
|
||||
const res = await myperson.save();
|
||||
|
||||
if (sendnews) {
|
||||
// Send to the Admin an Email
|
||||
this.sendEmail_base('admin/added_to_newsletter/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '');
|
||||
|
||||
if (tools.isManagAndAdminDifferent(idapp)) {
|
||||
this.sendEmail_base('admin/added_to_newsletter/' + lang, tools.getManagerEmailByIdApp(idapp), mylocalsconf, '');
|
||||
}
|
||||
}
|
||||
|
||||
return { code: server_constants.RIS_SUBSCRIBED_OK, msg: server_constants.RIS_SUBSCRIBED_MSG[lang] }
|
||||
} else {
|
||||
// Already Esist
|
||||
return {
|
||||
code: server_constants.RIS_SUBSCRIBED_ALREADYEXIST,
|
||||
msg: server_constants.RIS_SUBSCRIBED_MSG_ALREADYEXIST[lang]
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Remove_from_MailingList: async function (lang, hashemail, idapp) {
|
||||
|
||||
// console.log('idapp', idapp, tools.getNomeAppByIdApp(idapp));
|
||||
|
||||
// Check if exist to the Mailing List
|
||||
if (await MailingList.isUnsubscribed(idapp, hashemail)) {
|
||||
return {
|
||||
myperson: null,
|
||||
code: server_constants.RIS_UNSUBSCRIBED_ALREADY_DONE,
|
||||
msg: server_constants.RIS_UNSUBSCRIBED_MSG_ALREADY_DONE[lang]
|
||||
}
|
||||
}
|
||||
|
||||
const fields_to_update = {
|
||||
statesub: false
|
||||
};
|
||||
let myperson = await MailingList.findOneAndUpdate({
|
||||
idapp,
|
||||
hash: hashemail
|
||||
}, { $set: fields_to_update }, { new: false });
|
||||
if (myperson) {
|
||||
return { myperson, code: server_constants.RIS_UNSUBSCRIBED_OK, msg: '' }
|
||||
|
||||
} else {
|
||||
// Not found !
|
||||
return {
|
||||
myperson: null,
|
||||
code: server_constants.RIS_UNSUBSCRIBED_NOT_EXIST,
|
||||
msg: server_constants.RIS_UNSUBSTR_NOT_EXIST[lang]
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
fieldsloop: function (myvar) {
|
||||
const baseurl = tools.getHostByIdApp(mylocalsconf.idapp);
|
||||
const urlunsibscribe = baseurl + '/unsubscribe?em=' + mylocalsconf.hashemail + '&mc=' + mylocalsconf.dataemail.mailchimpactive + '&email=' + mylocalsconf.emailto;
|
||||
|
||||
out = myvar.replace('{urlunsubscribe}', urlunsibscribe);
|
||||
out = out.replace('{email}', mylocalsconf.emailto);
|
||||
out = out.replace('{name}', mylocalsconf.name);
|
||||
out = out.replace('{surname}', mylocalsconf.surname);
|
||||
|
||||
return out
|
||||
},
|
||||
|
||||
replacefields: function (mylocalsconf) {
|
||||
|
||||
mylocalsconf.dataemail.disclaimer_out = this.fieldsloop(mylocalsconf.dataemail.disclaimer);
|
||||
mylocalsconf.dataemail.disc_bottom_out = this.fieldsloop(mylocalsconf.dataemail.disc_bottom);
|
||||
mylocalsconf.dataemail.templ.testoheadermail = this.fieldsloop(mylocalsconf.dataemail.templ.testoheadermail);
|
||||
|
||||
},
|
||||
|
||||
getdataemail: async (idapp) => {
|
||||
console.log('getdataemail');
|
||||
const mydata = {
|
||||
testomail: await Settings.getValDbSettings(idapp, 'EMAIL_TEXT'),
|
||||
content_after_events: await Settings.getValDbSettings(idapp, 'TEXT_AFTER_EV'),
|
||||
mailchimpactive: tools.BoolToInt(await Settings.getValDbSettings(idapp, 'MAILCHIMP_ON')),
|
||||
urltwitter: await Settings.getValDbSettings(idapp, 'URL_TWITTER'),
|
||||
urlfb: await Settings.getValDbSettings(idapp, 'URL_FACEBOOK'),
|
||||
urlyoutube: await Settings.getValDbSettings(idapp, 'URL_YOUTUBE'),
|
||||
urlinstagram: await Settings.getValDbSettings(idapp, 'URL_INSTAGRAM'),
|
||||
textpromo: await Settings.getValDbSettings(idapp, 'TEXT_PROMO'),
|
||||
disclaimer: await Settings.getValDbSettings(idapp, 'TEXT_DISCLAIMER'),
|
||||
disc_bottom: await Settings.getValDbSettings(idapp, 'TEXT_DISC_BOTTOM'),
|
||||
firma: await Settings.getValDbSettings(idapp, 'TEXT_SIGN'),
|
||||
|
||||
arrdiscipline: await Discipline.getDisciplineforNewsletter(idapp),
|
||||
disc_title: await Settings.getValDbSettings(idapp, 'DISC_TITLE'),
|
||||
height_logo: await Settings.getValDbSettings(idapp, 'HEIGHT_LOGO'),
|
||||
from: await Settings.getValDbSettings(idapp, 'EMAIL_FROM'),
|
||||
pwd_from: await Settings.getValDbSettings(idapp, 'PWD_FROM'),
|
||||
email_service: await Settings.getValDbSettings(idapp, 'EMAIL_SERVICE_SEND'),
|
||||
email_port: await Settings.getValDbSettings(idapp, 'EMAIL_PORT'),
|
||||
templemail_id: await Settings.getValDbSettings(idapp, 'TEMPLEMAIL_ID'),
|
||||
};
|
||||
|
||||
console.log(mydata.templemail_id);
|
||||
mydata.templ = await TemplEmail.findOne({_id: mydata.templemail_id });
|
||||
console.log(mydata.templ);
|
||||
|
||||
return mydata
|
||||
},
|
||||
|
||||
getTransport: () => {
|
||||
// Create Transport
|
||||
let smtpTransport = null;
|
||||
|
||||
if (mylocalsconf.dataemail.email_service !== 'gmail') {
|
||||
smtpTransport = nodemailer.createTransport({
|
||||
host: mylocalsconf.dataemail.email_service,
|
||||
port: mylocalsconf.dataemail.email_port,
|
||||
auth: {
|
||||
user: process.env.EMAIL_FROM,
|
||||
pass: process.env.EMAIL_PW
|
||||
}
|
||||
});
|
||||
} else if (mylocalsconf.dataemail.email_service === 'gmail') {
|
||||
smtpTransport = {
|
||||
service: 'gmail', //'Gmail',
|
||||
auth: {
|
||||
user: mylocalsconf.dataemail.from,
|
||||
pass: mylocalsconf.dataemail.pwd_from
|
||||
}
|
||||
};
|
||||
} else {
|
||||
smtpTransport = {
|
||||
service: 'gmail',
|
||||
auth: {
|
||||
user: process.env.EMAIL_FROM,
|
||||
pass: process.env.EMAIL_PW
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return smtpTransport;
|
||||
},
|
||||
|
||||
sendEmail_Newsletter_Events: async function (lang, idapp, id_newstosent) {
|
||||
@@ -298,30 +461,54 @@ module.exports = {
|
||||
console.log('INIZIO - sendEmail_Newsletter_Events', tools.getNomeAppByIdApp(idapp));
|
||||
|
||||
//++Todo Extract List Email to send
|
||||
const userstosend = await MailingList.findAllIdApp(idapp);
|
||||
const userstosend = await MailingList.findAllIdAppSubscribed(idapp);
|
||||
|
||||
const myarrevents = await MyEvent.getLastEvents(idapp);
|
||||
|
||||
mylocalsconf = {
|
||||
locale: lang,
|
||||
idapp,
|
||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||
arrevents: myarrevents
|
||||
arrevents: myarrevents,
|
||||
baseurl: tools.getHostByIdApp(idapp),
|
||||
dataemail: await this.getdataemail(idapp)
|
||||
};
|
||||
|
||||
|
||||
const mynewsrec = await Newstosent.findOne({ _id: id_newstosent });
|
||||
|
||||
mynewsrec.numemail_tot = userstosend.length;
|
||||
|
||||
const smtpTransport = this.getTransport();
|
||||
|
||||
// LOOP USERS EMAIL
|
||||
for (const user of userstosend) {
|
||||
|
||||
if (process.env.DEBUG)
|
||||
await tools.snooze(10000);
|
||||
|
||||
activate = await Newstosent.isActivated(id_newstosent);
|
||||
|
||||
if (!activate) {
|
||||
// Invio Newsletter Abortito!
|
||||
mynewsrec.error_job = 'Invio Newsletter abortito alle ore ' + new Date().toDateString();
|
||||
await mynewsrec.save();
|
||||
console.log('*** L\'Invio della Newsletter è stato fermato ! ');
|
||||
return false
|
||||
}
|
||||
|
||||
mylocalsconf.name = user.name;
|
||||
mylocalsconf.surname = user.surname;
|
||||
mylocalsconf.emailto = user.email;
|
||||
mylocalsconf.hashemail = tools.getHash(user.email);
|
||||
|
||||
await replacefields(mylocalsconf);
|
||||
|
||||
// If has already sent, don't send it again!
|
||||
if (user.lastid_newstosent !== id_newstosent.toString()) {
|
||||
// Send Email to the User
|
||||
console.log('-> Invio Email (', mynewsrec.numemail_sent, '/', mynewsrec.numemail_tot, ')');
|
||||
const esito = this.sendEmail_base('newsletter/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '');
|
||||
const esito = this.sendEmail_base('newsletter/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '', smtpTransport);
|
||||
|
||||
//Put the result in the database, to check if is sent or not.
|
||||
const updateml = await MailingList.findOneAndUpdate({
|
||||
@@ -330,14 +517,15 @@ module.exports = {
|
||||
}, { $set: { lastid_newstosent: ObjectID(id_newstosent) } }, { new: false });
|
||||
|
||||
|
||||
//Delay for send email...
|
||||
await tools.snooze(process.env.DELAY_SENDEMAIL);
|
||||
|
||||
|
||||
mynewsrec.lastemailsent_Job = new Date();
|
||||
mynewsrec.numemail_sent += 1;
|
||||
|
||||
const recsaved = await mynewsrec.save();
|
||||
|
||||
//Delay for send email...
|
||||
await tools.snooze(process.env.DELAY_SENDEMAIL);
|
||||
|
||||
if (mynewsrec.numemail_sent === mynewsrec.numemail_tot) {
|
||||
mynewsrec.datefinishJob = new Date();
|
||||
mynewsrec.finish_job = true;
|
||||
@@ -374,42 +562,52 @@ module.exports = {
|
||||
},
|
||||
|
||||
sendNewsletter: async function (rec) {
|
||||
if (rec) {
|
||||
// Start the job
|
||||
myjobnews = await Newstosent.findOne({ _id: rec._id });
|
||||
if (!!myjobnews) {
|
||||
myjobnews.starting_job = true;
|
||||
myjobnews.datestartJob = new Date();
|
||||
|
||||
// Start the job
|
||||
myjobnews = await Newstosent.findOne({ _id: rec._id });
|
||||
if (!!myjobnews) {
|
||||
myjobnews.starting_job = true;
|
||||
myjobnews.datestartJob = new Date();
|
||||
myjobnews.save()
|
||||
.then((ris) => {
|
||||
|
||||
myjobnews.save()
|
||||
.then((ris) => {
|
||||
|
||||
this.sendEmail_Newsletter_Events("it", '2', rec._id);
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
});
|
||||
this.sendEmail_Newsletter_Events("it", '2', rec._id);
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
testemail: async function (idapp, lang) {
|
||||
testemail: async function (idapp, lang, previewonly) {
|
||||
|
||||
const myarrevents = await MyEvent.getLastEvents(idapp);
|
||||
const myemail = await Settings.getValDbSettings(idapp, 'EMAIL_TEST');
|
||||
mylocalsconf = {
|
||||
locale: lang,
|
||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||
arrevents: myarrevents,
|
||||
name: 'Paolo',
|
||||
surname: 'Arena',
|
||||
emailto: "pa.oloarena77@gmail.com",
|
||||
name: 'TestNome',
|
||||
surname: 'TestCognome',
|
||||
emailto: myemail,
|
||||
idapp,
|
||||
baseurl: tools.getHostByIdApp(idapp),
|
||||
hashemail: tools.getHash(myemail),
|
||||
};
|
||||
|
||||
console.log('-> Invio Email TEST a', mylocalsconf.emailto);
|
||||
return this.sendEmail_base('newsletter/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '');
|
||||
mylocalsconf.dataemail = await this.getdataemail(idapp);
|
||||
|
||||
this.replacefields(mylocalsconf);
|
||||
|
||||
const smtpTransport = this.getTransport();
|
||||
|
||||
console.log('-> Invio Email TEST a', mylocalsconf.emailto, 'previewonly', previewonly);
|
||||
return this.sendEmail_base('newsletter/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '', smtpTransport, previewonly);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
;
|
||||
|
||||
Reference in New Issue
Block a user