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 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 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', 'adresse', '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""" print(" ### Adresse = "+str(diction['adresse']) ) adresse_session = str(diction['adresse']) trim_adr = str(diction['adresse']).strip().replace(" ","").lower() if (trim_adr == "enligne"): adresse_session = "enligne" """ 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() # 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']), "email": str(diction['email'])}, } sourceHtml = template.render(params=body["params"]) msg.set_content(sourceHtml, subtype='html') msg['From'] = 'cbalde@mysy-training.com' msg['Cc'] = '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 " """ 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', 'adresse', '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 ") # on rentre les renseignements pris sur le site du fournisseur msg = MIMEMultipart("alternative") msg['Subject'] = 'MySy Training : pre-inscription formation' msg['From'] = 'contact@mysy-training.com' msg['To'] = str(diction['email']) to = [{"email": str(diction['email'])}] bcc = [{"email": "contact@mysy-training.com"}] print(" ### Adresse = "+str(diction['adresse']) ) """send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(template_id=2, 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 ) """ # 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_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(diction['adresse']), "title": str(diction['title']), "email": str(diction['email'])}, } sourceHtml = template.render(params=body["params"]) msg.set_content(sourceHtml, subtype='html') msg['From'] = 'cbalde@mysy-training.com' msg['Cc'] = '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 "