from __future__ import print_function import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders import locale import datetime import inspect import sys, os from email import encoders from bson import ObjectId from pymongo import ReturnDocument from xhtml2pdf import pisa import prj_common as mycommon import GlobalVariable as MYSY_GV from dateutil import tz import pytz import sib_api_v3_sdk from sib_api_v3_sdk.rest import ApiException import jinja2 from email.message import EmailMessage from email.mime.text import MIMEText from email import encoders configuration = sib_api_v3_sdk.Configuration() configuration.api_key['api-key'] = MYSY_GV.SENDINBLUE_API_KEY_SUPPORT api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration)) locale.setlocale(category=locale.LC_ALL, locale='fr_FR.utf8') smtp_address = 'smtp-relay.sendinblue.com' port = 587 sender = 'support@mysy-training.com' receiver = ["cbalde3@mysy-training.com","billardman1@gmail.com"] user = 'support@mysy-training.com' password = 'Tut29488!+' """ Envoi de l'email de confirmation d'inscription à une formation """ def incription_training_confirmation_mail(diction): try: print(" ### incription_training_confirmation_mail = "+str(diction)) """ Verification de la liste des champs obligatoires """ field_list_obligatoire = ['nom', 'prenom', 'email', 'date_du', 'date_au', 'title'] for val in field_list_obligatoire: if val not in diction: mycommon.myprint( str(inspect.stack()[0][3]) + " - : La valeur '" + val + "' n'est pas presente dans liste ") return False, "Impossible d'envoyer l'email de confirmation de l'inscritpion" tomorrow = datetime.date.today() + datetime.timedelta(days=1) tomorrow_day = tomorrow.strftime("%A") print("debut envoi mail de test ") """ Verification si c'est une formation en ligne. auquel cas, l'adresse est du type : enligne""" adresse = "" if ("adresse" in diction.keys()): if diction['adresse']: adresse = diction['adresse'] adresse_session = adresse """ send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(template_id=1, params={ "nom": str(diction['nom']), "prenom":str(diction['prenom']), "date_du": str(diction['date_du']), "date_fin": str(diction['date_au']), "adresse": str(diction['adresse']), "title":str(diction['title'])}, to=to, bcc=bcc ) """ print("debut envoi mail de test ") # on rentre les renseignements pris sur le site du fournisseur # msg = MIMEMultipart("alternative") smtpserver = smtplib.SMTP(MYSY_GV.O365_SMTP_COUNT_smtpsrv, MYSY_GV.O365_SMTP_COUNT_port) msg = EmailMessage() local_code_postal = "" if ("code_postal" in diction.keys()): if diction['code_postal']: local_code_postal = diction['code_postal'] local_ville = "" if ("ville" in diction.keys()): if diction['ville']: local_ville = diction['ville'] local_distantiel = "" if ("distantiel" in diction.keys()): if diction['distantiel']: local_distantiel = diction['distantiel'] local_presentiel = "" if ("presentiel" in diction.keys()): if diction['presentiel']: local_presentiel = diction['presentiel'] local_formateur = "" if ("formateur" in diction.keys()): if diction['formateur']: local_formateur = diction['formateur'] # JINJA2 templateLoader = jinja2.FileSystemLoader(searchpath="./") templateEnv = jinja2.Environment(loader=templateLoader) TEMPLATE_FILE = "Template/MySy_confirmation_inscription_formation_tpl.html" template = templateEnv.get_template(TEMPLATE_FILE) # This data can come from database query body = { "params" :{"nom": str(diction['nom']), "prenom": str(diction['prenom']), "date_du": str(diction['date_du']), "date_fin": str(diction['date_au']), "adresse": str(adresse_session), "title": str(diction['title']), "code_postal": str(local_code_postal), "ville": str(local_ville), "distantiel": str(local_distantiel), "presentiel": str(local_presentiel), "formateur": str(local_formateur), "email": str(diction['email'])}, } sourceHtml = template.render(params=body["params"]) msg.set_content(sourceHtml, subtype='html') msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User msg['Bcc'] = 'contact@mysy-training.com' msg['Subject'] = '[MySy Training]: Confirmation inscription formation' msg['To'] = str(diction['email']) smtpserver.ehlo() smtpserver.starttls() smtpserver.login(MYSY_GV.O365_SMTP_COUNT_user, MYSY_GV.O365_SMTP_COUNT_password) val = smtpserver.send_message(msg) smtpserver.close() print(" Email confirlation envoyé " + str(val)) return True, " Email confirlation inscritpion bien envoyé" except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) return False, " Impossible d'envoyer l'email de notification " """ Envoi de l'email de refus d'une d'inscription à une formation """ def incription_training_refused_mail(diction): try: print(" ### incription_training_refused_mail = "+str(diction)) """ Verification de la liste des champs obligatoires """ field_list_obligatoire = ['nom', 'prenom', 'email', 'date_du', 'date_au', 'title', ] for val in field_list_obligatoire: if val not in diction: mycommon.myprint( str(inspect.stack()[0][3]) + " - : La valeur '" + val + "' n'est pas presente dans liste ") return False, "Impossible d'envoyer l'email de refus de l'inscritpion" tomorrow = datetime.date.today() + datetime.timedelta(days=1) tomorrow_day = tomorrow.strftime("%A") print("debut envoi mail de test ") """ Verification si c'est une formation en ligne. auquel cas, l'adresse est du type : enligne""" my_adress = "" if ("adresse" in diction.keys()): if diction['adresse']: my_adress = diction['adresse'] presentiel = "" if ("presentiel" in diction.keys()): if diction['presentiel']: presentiel = diction['presentiel'] distantiel = "" if ("distantiel" in diction.keys()): if diction['distantiel']: distantiel = diction['distantiel'] adresse_session = my_adress # on rentre les renseignements pris sur le site du fournisseur # msg = MIMEMultipart("alternative") smtpserver = smtplib.SMTP(MYSY_GV.O365_SMTP_COUNT_smtpsrv, MYSY_GV.O365_SMTP_COUNT_port) msg = EmailMessage() # JINJA2 templateLoader = jinja2.FileSystemLoader(searchpath="./") templateEnv = jinja2.Environment(loader=templateLoader) TEMPLATE_FILE = "Template/MySy_refus_inscription_formation_tpl.html" template = templateEnv.get_template(TEMPLATE_FILE) # This data can come from database query body = {} if ("comment" in diction.keys()): if diction['comment']: body = { "params" :{"nom": str(diction['nom']), "prenom": str(diction['prenom']), "date_du": str(diction['date_du']), "date_fin": str(diction['date_au']), "adresse": str(adresse_session), "title": str(diction['title']), "email": str(diction['email']), "distantiel": str(diction['distantiel']), "presentiel": str(diction['presentiel']), "comment": str(diction['comment'])}, } else: body = { "params": {"nom": str(diction['nom']), "prenom": str(diction['prenom']), "date_du": str(diction['date_du']), "date_fin": str(diction['date_au']), "adresse": str(adresse_session), "title": str(diction['title']), "email": str(diction['email'])}, } sourceHtml = template.render(params=body["params"]) msg.set_content(sourceHtml, subtype='html') msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User msg['Bcc'] = 'contact@mysy-training.com' msg['Subject'] = '[MySy Training]: Inscription formation refusée' msg['To'] = str(diction['email']) smtpserver.ehlo() smtpserver.starttls() smtpserver.login(MYSY_GV.O365_SMTP_COUNT_user, MYSY_GV.O365_SMTP_COUNT_password) val = smtpserver.send_message(msg) smtpserver.close() print(" Email confirlation envoyé " + str(val)) return True, " Email confirlation inscritpion bien envoyé" except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) return False, " Impossible d'envoyer l'email de notification " """ Email de pre-inscription """ def Pre_incription_training_confirmation_mail(diction): try: print(" ### Preincription_training_confirmation_mail = "+str(diction)) """ Verification de la liste des champs obligatoires """ field_list_obligatoire = ['nom', 'prenom', 'email', 'date_du', 'date_au', 'title', 'distantiel', 'presentiel'] for val in field_list_obligatoire: if val not in diction: mycommon.myprint( str(inspect.stack()[0][3]) + " - : La valeur '" + val + "' n'est pas presente dans liste ") return False, "Impossible d'envoyer l'email de confirmation de l'inscritpion" tomorrow = datetime.date.today() + datetime.timedelta(days=1) tomorrow_day = tomorrow.strftime("%A") print("debut envoi mail de test ") # on rentre les renseignements pris sur le site du fournisseur msg = MIMEMultipart("alternative") msg['Subject'] = 'MySy Training : pre-inscription formation' msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User msg['To'] = str(diction['email']) # msg = MIMEMultipart("alternative") msg = EmailMessage() smtpserver = smtplib.SMTP(MYSY_GV.O365_SMTP_COUNT_smtpsrv, MYSY_GV.O365_SMTP_COUNT_port) local_adress = "" if ("adresse" in diction.keys()): if diction['adresse']: local_adress = diction['adresse'] # JINJA2 templateLoader = jinja2.FileSystemLoader(searchpath="./") templateEnv = jinja2.Environment(loader=templateLoader) TEMPLATE_FILE = "Template/MySy_confirmation_Pre_inscription_formation_tpl.html" template = templateEnv.get_template(TEMPLATE_FILE) # This data can come from database query body = { "params":{"nom": str(diction['nom']), "prenom": str(diction['prenom']), "date_du": str(diction['date_du']), "date_fin": str(diction['date_au']), "distantiel": str(diction['distantiel']), "presentiel": str(diction['presentiel']), "title": str(diction['title']), "code_postal": str(diction['code_postal']), "ville": str(diction['ville']), "adresse":str(local_adress), "email": str(diction['email'])}, } sourceHtml = template.render(params=body["params"]) msg.set_content(sourceHtml, subtype='html') msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User msg['Bcc'] = 'contact@mysy-training.com' msg['Subject'] = '[MySy Training] : pre-inscription formation' msg['To'] = str(diction['email']) smtpserver.ehlo() smtpserver.starttls() smtpserver.login(MYSY_GV.O365_SMTP_COUNT_user, MYSY_GV.O365_SMTP_COUNT_password) val = smtpserver.send_message(msg) smtpserver.close() print(" Email envoyé " + str(val)) return True, " Email bien envoyé" except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) return False, " Impossible d'envoyer l'email de notification " """ Cet email envoie une notfication au l'institut de formation (partenair) Pour l'informer qu'un utilisateur s'est pre-inscrit """ def Notification_partner_Pre_incription_mail(diction): try: print(" ### Notification_partner_Pre_incription_mail = "+str(diction)) """ Verification de la liste des champs obligatoires """ field_list_obligatoire = ['nom', 'prenom', 'email', 'date_du', 'date_au', 'title', 'partner_mail', 'telephone'] for val in field_list_obligatoire: if val not in diction: mycommon.myprint( str(inspect.stack()[0][3]) + " - : La valeur '" + val + "' n'est pas presente dans liste ") return False, "Impossible d'envoyer l'email de confirmation de l'inscritpion" local_adress = "" if ("adresse" in diction.keys()): if diction['adresse']: local_adress = diction['adresse'] local_employeur = "" if ("employeur" in diction.keys()): if diction['employeur']: local_employeur = diction['employeur'] local_ville = "" if ("ville" in diction.keys()): if diction['ville']: local_ville = diction['ville'] local_code_postal = "" if ("code_postal" in diction.keys()): if diction['code_postal']: local_code_postal = diction['code_postal'] tomorrow = datetime.date.today() + datetime.timedelta(days=1) tomorrow_day = tomorrow.strftime("%A") print("debut envoi mail de test ") # on rentre les renseignements pris sur le site du fournisseur msg = MIMEMultipart("alternative") msg['Subject'] = 'MySy Training : pre-inscription formation' msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User msg['To'] = str(diction['email']) #print(" ### Adresse = "+str(diction['adresse']) ) # msg = MIMEMultipart("alternative") msg = EmailMessage() smtpserver = smtplib.SMTP(MYSY_GV.O365_SMTP_COUNT_smtpsrv, MYSY_GV.O365_SMTP_COUNT_port) # JINJA2 templateLoader = jinja2.FileSystemLoader(searchpath="./") templateEnv = jinja2.Environment(loader=templateLoader) TEMPLATE_FILE = "Template/MySy_Notif_Partner_Pre_inscription_formation_tpl.html" template = templateEnv.get_template(TEMPLATE_FILE) # This data can come from database query body = { "params":{"nom": str(diction['nom']), "prenom": str(diction['prenom']), "date_du": str(diction['date_du']), "date_fin": str(diction['date_au']), "adresse": str(local_adress), "employeur": str(local_employeur), "telephone": str(diction['telephone']), "distantiel": str(diction['distantiel']), "presentiel": str(diction['presentiel']), "title": str(diction['title']), "code_postal": str(local_code_postal), "ville": str(local_ville), "email": str(diction['email'])}, } sourceHtml = template.render(params=body["params"]) msg.set_content(sourceHtml, subtype='html') msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User msg['Cc'] = 'contact@mysy-training.com, billardman01@hotmail.com' msg['Subject'] = '[MySy Training] : pre-inscription à la formation '+str(diction['title']) msg['To'] = str(diction['partner_mail']) smtpserver.ehlo() smtpserver.starttls() smtpserver.login(MYSY_GV.O365_SMTP_COUNT_user, MYSY_GV.O365_SMTP_COUNT_password) val = smtpserver.send_message(msg) smtpserver.close() print(" Email envoyé " + str(val)) return True, " Email bien envoyé" except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) return False, " Impossible d'envoyer l'email de notification " """ Envoi de l'email d'evaluation """ def Evaluation_training_confirmation_mail(diction): try: print(" ### Evaluation_training_confirmation_mail = " + str(diction)) """ Verification de la liste des champs obligatoires """ field_list_obligatoire = ['nom', 'prenom', 'email', 'date_du', 'date_au', 'adresse', 'title', 'token_eval', 'internal_url'] for val in field_list_obligatoire: if val not in diction: mycommon.myprint( str(inspect.stack()[0][3]) + " - : La valeur '" + val + "' n'est pas presente dans liste ") return False, "Impossible d'envoyer l'email de demande d'evaluation" tomorrow = datetime.date.today() + datetime.timedelta(days=1) tomorrow_day = tomorrow.strftime("%A") print("debut envoi mail de test ") # on rentre les renseignements pris sur le site du fournisseur msg = MIMEMultipart("alternative") msg['Subject'] = 'MySy Training : Evaluation formation' msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User msg['To'] = str(diction['email']) my_url = "" if (MYSY_GV.MYSY_ENV == "PROD"): my_url = "https://www.mysy-training.com/Display-Detail-formation/" + str(diction['internal_url'])+"/"+str(diction['token_eval']) elif (MYSY_GV.MYSY_ENV == "DEV"): my_url = "http://localhost:3009/Display-Detail-formation/" + str(diction['internal_url'])+"/"+str(diction['token_eval']) elif (MYSY_GV.MYSY_ENV == "REC"): my_url = "https://dev.mysy-training.com/Display-Detail-formation/" + str(diction['internal_url'])+"/"+str(diction['token_eval']) print(" ### Adresse = "+str(diction['adresse']) ) print(" #### my_url = ", my_url) # msg = MIMEMultipart("alternative") msg = EmailMessage() smtpserver = smtplib.SMTP(MYSY_GV.O365_SMTP_COUNT_smtpsrv, MYSY_GV.O365_SMTP_COUNT_port) # JINJA2 templateLoader = jinja2.FileSystemLoader(searchpath="./") templateEnv = jinja2.Environment(loader=templateLoader) TEMPLATE_FILE = "Template/MySy_Evaluation_Class_tpl.html" template = templateEnv.get_template(TEMPLATE_FILE) # This data can come from database query body = { "params":{"nom": str(diction['nom']), "prenom": str(diction['prenom']), "date_du": str(diction['date_du']), "date_fin": str(diction['date_au']), "adresse": str(diction['adresse']), "title": str(diction['title']), "presentiel": str(diction['presentiel']), "distantiel": str(diction['distantiel']), "email": str(diction['email']), "mysyurl": str(my_url)} } sourceHtml = template.render(params=body["params"]) msg.set_content(sourceHtml, subtype='html') msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User msg['Bcc'] = 'contact@mysy-training.com' msg['Subject'] = 'MySy Training : Evaluation formation : '+str(diction['title']) msg['To'] = str(diction['email']) smtpserver.ehlo() smtpserver.starttls() smtpserver.login(MYSY_GV.O365_SMTP_COUNT_user, MYSY_GV.O365_SMTP_COUNT_password) val = smtpserver.send_message(msg) smtpserver.close() print(" Email envoyé " + str(val)) return True, " Email bien envoyé" except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) return False, " Impossible d'envoyer l'email de demande d'evaluation " """ Cette fonction envoi l'attestion par email """ def SendAttestion_to_attendee_by_email(diction): try: """ Verification de la liste des champs obligatoires """ field_list_obligatoire = ['partner_recid', 'session_id', 'class_internal_url', 'class_title' , 'email_participant', 'attestation_tmpl'] for val in field_list_obligatoire: if val not in diction: mycommon.myprint( str(inspect.stack()[0][3]) + " - : La valeur '" + val + "' n'est pas presente dans liste ") return False, "Impossible d'envoyer l'attestation par email " print(' ### diction = ',str(diction)) msg = MIMEMultipart("alternative") ## Recuperation des données du partenaire local_partnair = MYSY_GV.dbname['partnair_account'].find_one({'recid':str(diction['partner_recid']), 'active':'1', 'locked':'0'}) if( local_partnair is None ): mycommon.myprint( str(inspect.stack()[0][3]) + " - Impossible de recuperer les données du partenaire") return False, " Impossible de recuperer les données du partenaire " mysy_partner_nom = "" if ("nom" in local_partnair.keys()): mysy_partner_nom = str(local_partnair['nom']) mysy_partner_num_declation = "" if ("num_nda" in local_partnair.keys()): mysy_partner_num_declation = str(local_partnair['num_nda']) mysy_partner_adr_city = "" if ("adr_city" in local_partnair.keys()): mysy_partner_adr_city = str(local_partnair['adr_city']) ## Recuperation des données de l'inscrit local_inscription = MYSY_GV.dbname['inscription'].find_one({'session_id': str(diction['session_id']), 'class_internal_url': str(diction['class_internal_url']), 'email':str(diction['email_participant'])}) if (local_inscription is None): mycommon.myprint( str(inspect.stack()[0][3]) + " - Impossible de recuperer les données du participants") return False, " Impossible de recuperer les données du participants " nom = "" if ("nom" in local_inscription.keys()): nom = str(local_inscription['nom']) prenom = "" if ("prenom" in local_inscription.keys()): prenom = str(local_inscription['prenom']) date_du = "" if ("date_du" in local_inscription.keys()): date_du = str(local_inscription['date_du'])[0:10] date_au = "" if ("date_au" in local_inscription.keys()): date_au = str(local_inscription['date_au'])[0:10] ville = "" if ("ville" in local_inscription.keys()): ville = str(local_inscription['ville']) code_postal = "" if ("code_postal" in local_inscription.keys()): code_postal = str(local_inscription['code_postal']) adresse = "" if ("adresse" in local_inscription.keys()): adresse = str(local_inscription['adresse']) ## Recuperation des données de la session local_session = MYSY_GV.dbname['session_formation'].find_one({'code_session': str(diction['session_id']), 'class_internal_url': str( diction['class_internal_url']), 'valide':'1'}) if (local_session is None): mycommon.myprint( str(inspect.stack()[0][3]) + " - Impossible de recuperer les données de la session") return False, " Impossible de recuperer les données de la session " formateur = "" if ("formateur" in local_session.keys()): formateur = str(local_session['formateur']) presentiel = "" if ("presentiel" in local_session.keys()): presentiel = str(local_session['presentiel']) distantiel = "" if ("distantiel" in local_session.keys()): distantiel = str(local_session['distantiel']) contenu_ftion = "" if ("contenu_ftion" in local_session.keys()): contenu_ftion = str(local_session['contenu_ftion']).replace('
', '').replace('
', '').replace("\r\n", '