Elyos_FI_Back_Office/email_mgt.py

361 lines
13 KiB
Python

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
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key'] = MYSY_GV.SENDINBLUE_API_KEY
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 = 'contact@mysy-training.com'
receiver = ["cbalde3@mysy-training.com","billardman1@gmail.com"]
user = 'contact@mysy-training.com'
password = 'yGmzqBfrjb3YCvQN'
def send_user_account_mail(message, account_mail):
try:
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 : Activez votre compte '
msg['From'] = 'contact@mysy-training.com'
msg['To'] = str(account_mail)
to = [{"email":str(account_mail)}]
bcc = [{"email": "contact@mysy-training.com"}]
my_url = ""
if (MYSY_GV.MYSY_ENV == "PROD"):
my_url = "https://www.mysy-training.com/myclass/api/valide_user_account/"+str(message)
elif (MYSY_GV.MYSY_ENV == "DEV"):
my_url = "http://localhost:5000/myclass/api/valide_user_account/"+str(message)
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(template_id=15, params={"mysyurl": my_url,
"user_name": str(account_mail),
"id": "122CSR_id"}, to=to, bcc=bcc
)
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
return True
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 "
'''
Cette fonction envoie un mail de remerciement apres l'activation
du compte d'un utilisateur
'''
def send_user_thks_mail(message, account_mail):
try:
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 : Votre compte est actif '
msg['From'] = 'contact@mysy-training.com'
msg['To'] = str(account_mail)
to = [{"email": str(account_mail)}]
bcc = [{"email": "contact@mysy-training.com"}]
my_url = ""
if (MYSY_GV.MYSY_ENV == "PROD"):
my_url = "https://www.mysy-training.com/myclass/api/valide_user_account/" + str(message)
elif (MYSY_GV.MYSY_ENV == "DEV"):
my_url = "http://localhost:5000/myclass/api/valide_user_account/" + str(message)
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(template_id=16, params={ "user_name": str(account_mail),},
to=to, bcc=bcc )
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
return True
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 "
def send_partner_account_mail(message, account_mail):
try:
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
tomorrow_day = tomorrow.strftime("%A")
receiver = [str(account_mail)]
toaddrs = ", ".join(receiver)
my_url = ""
if (MYSY_GV.MYSY_ENV == "PROD"):
my_url = "https://www.mysy-training.com/myclass/api/valide_partnair_account/" + str(message)
elif (MYSY_GV.MYSY_ENV == "DEV"):
my_url = "http://localhost:5000/myclass/api/valide_partnair_account/"+str(message)
print(" mail enoye à toaddrs : "+toaddrs)
print("debut envoi mail de test ")
# on rentre les renseignements pris sur le site du fournisseur
msg = MIMEMultipart("alternative")
msg['Subject'] = '[MySy Training]: activation de votre compte PRO'
msg['From'] = 'contact@mysy-training.com'
msg['To'] = str(toaddrs)
msg['Cc'] = 'contact@mysy-training.com'
to = [{"email": str(account_mail)}]
bcc = [{"email": "contact@mysy-training.com"}]
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(template_id=17, params={"mysyurl": my_url,
"user_name": str(account_mail),
}, to=to, bcc=bcc
)
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
return True
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"
'''
Envoie de l'email de reinitialisation du mot de passe du user
'''
def send_user_init_pwd_mail(token, account_mail):
try:
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 Technology] : Reinitialisez votre mot de passe'
msg['From'] = 'contact@mysy-training.com'
msg['To'] = str(account_mail)
my_url = ""
if (MYSY_GV.MYSY_ENV == "PROD"):
my_url = "https://www.mysy-training.com/ResetUserPwd/" + str(token)
elif (MYSY_GV.MYSY_ENV == "DEV"):
my_url = "http://localhost:3009/ResetUserPwd/" + str(token)
to = [{"email": str(account_mail)}]
bcc = [{"email": "contact@mysy-training.com"}]
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(template_id=19, params={"mysyurl": my_url,
"user_name": str(account_mail),
}, to=to, bcc=bcc
)
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
return True
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 "
'''
Envoie de l'email de suppression du compte utilisateur
'''
def send_user_delete_account(token, account_mail):
try:
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
tomorrow_day = tomorrow.strftime("%A")
msg = MIMEMultipart("alternative")
msg['Subject'] = '[MySy Training Technology] : Suppression de votre'
msg['From'] = 'contact@mysy-training.com'
msg['To'] = str(account_mail)
my_url = ""
if( MYSY_GV.MYSY_ENV == "PROD"):
my_url = "https://www.mysy-training.com/DeleteUserAccount/" + str(token)
elif (MYSY_GV.MYSY_ENV == "DEV"):
my_url = "http://localhost:3009/DeleteUserAccount/" + str(token)
to = [{"email": str(account_mail)}]
bcc = [{"email": "contact@mysy-training.com"}]
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(template_id=20, params={"mysyurl": my_url,
"user_name": str(account_mail),
}, to=to, bcc=bcc
)
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
return True
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 "
'''
Cette fonction envoir n'import quel email avec les varibable :
- recever_email
- objet_mail
- message_mail
'''
def SendGenericEmail(mail_recever=None, mail_object=None, mail_message=None):
try:
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 Technology] : '+str(mail_object)
msg['From'] = MYSY_GV.INTERNAL_MAIL_SENDER
msg['To'] = str(mail_recever)
html = '''
<!DOCTYPE html>
<html>
<body>
<div style="background-color:#eee;padding:0px;text-align:center;width:100%">
<h2 style="font-family:Georgia, 'Times New Roman', Times, serif;color#454349;">
<img src="http://88.170.110.220/img/MYSY-LOGO-BLUE.png" alt="Mysy Training Logo" width="110px" height="70px"/> </h2>
</div>
<div style="padding:20px 0px;text-align:center;font-family:Georgia, 'Times New Roman', Times, serif;color#454349;font-size:2rem;">
<div style="width:100%">
<div style="text-align:center;">
Bonjour
<div style="padding:20px 0px;text-align:center;font-family:Georgia, 'Times New Roman', Times, serif;color#454349;font-size:1.2rem;">
Message de <font color="green">MySy-Training Technology </font>.<br/>
{mymessage}
</div>
</body>
</html> '''.format(mymessage=mail_message)
html_mime = MIMEText(html, 'html')
# msg.attach(texte_mime)
msg.attach(html_mime)
print("html_mime =" + str(html))
toaddr = str(mail_recever)
cc = ['cbalde@mysy-training.com']
toaddrs = [toaddr] + cc
# msg.attach(MIMEText(open(filename).read()))
with smtplib.SMTP(smtp_address, port) as server:
server.starttls() # Secure the connection
server.login(user, password)
server.sendmail(sender, toaddrs, msg.as_string())
mycommon.myprint("mail successfully sent to " + str(toaddrs))
return True
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
print(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
return False
'''
Apres l'activation du compte pro,
cet email envoie le token et mot de passe temporaire à l'utilisateur
'''
def Pro_Account_Token_Pass(account_mail, my_token):
try:
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
tomorrow_day = tomorrow.strftime("%A")
receiver = [str(account_mail)]
toaddrs = ", ".join(receiver)
print(" mail enoye à toaddrs : " + toaddrs)
print("debut envoi mail de test ")
# on rentre les renseignements pris sur le site du fournisseur
msg = MIMEMultipart("alternative")
msg['Subject'] = '[MySy Training]: votre compte PRO est pret'
msg['From'] = 'contact@mysy-training.com'
msg['To'] = str(toaddrs)
msg['Cc'] = 'contact@mysy-training.com'
cle = my_token
print(" mail enoye à toaddrs : " + str(msg['To']))
to = [{"email": str(account_mail)}]
bcc = [{"email": "contact@mysy-training.com"}]
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(template_id=18, params={"user_name": str(account_mail), "secrete":str(my_token) },
to=to, bcc=bcc)
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
return True
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
print(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
return False