29/07/2024 - 12h
parent
8cfff6e486
commit
8dfbd7e8a0
|
@ -1,9 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="c6d0259a-16e1-410d-91a1-830590ee2a08" name="Changes" comment="23/07/2024 - 12h">
|
||||
<list default="true" id="c6d0259a-16e1-410d-91a1-830590ee2a08" name="Changes" comment="26/07/2024 - 12h">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Inscription_mgt.py" beforeDir="false" afterPath="$PROJECT_DIR$/Inscription_mgt.py" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/GlobalVariable.py" beforeDir="false" afterPath="$PROJECT_DIR$/GlobalVariable.py" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Log/log_file.log" beforeDir="false" afterPath="$PROJECT_DIR$/Log/log_file.log" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/email_mgt.py" beforeDir="false" afterPath="$PROJECT_DIR$/email_mgt.py" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
|
||||
|
@ -76,13 +76,6 @@
|
|||
<option name="presentableId" value="Default" />
|
||||
<updated>1680804787304</updated>
|
||||
</task>
|
||||
<task id="LOCAL-00310" summary="sdsqq">
|
||||
<created>1716399333864</created>
|
||||
<option name="number" value="00310" />
|
||||
<option name="presentableId" value="LOCAL-00310" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1716399333865</updated>
|
||||
</task>
|
||||
<task id="LOCAL-00311" summary="sdsqq">
|
||||
<created>1716483578232</created>
|
||||
<option name="number" value="00311" />
|
||||
|
@ -419,7 +412,14 @@
|
|||
<option name="project" value="LOCAL" />
|
||||
<updated>1721725677746</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="359" />
|
||||
<task id="LOCAL-00359" summary="26/07/2024 - 12h">
|
||||
<created>1721997046281</created>
|
||||
<option name="number" value="00359" />
|
||||
<option name="presentableId" value="LOCAL-00359" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1721997046282</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="360" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="Vcs.Log.Tabs.Properties">
|
||||
|
@ -461,7 +461,6 @@
|
|||
</option>
|
||||
</component>
|
||||
<component name="VcsManagerConfiguration">
|
||||
<MESSAGE value="11/06/2024 - 23h30" />
|
||||
<MESSAGE value="12/06/2024 - 21h30" />
|
||||
<MESSAGE value="15/06/2024 - 21h30" />
|
||||
<MESSAGE value="17/06/2024 - 21h30" />
|
||||
|
@ -486,6 +485,7 @@
|
|||
<MESSAGE value="17/07/2024 - 11h30sdf" />
|
||||
<MESSAGE value="21/07/2024 - 12h" />
|
||||
<MESSAGE value="23/07/2024 - 12h" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="23/07/2024 - 12h" />
|
||||
<MESSAGE value="26/07/2024 - 12h" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="26/07/2024 - 12h" />
|
||||
</component>
|
||||
</project>
|
|
@ -736,4 +736,11 @@ Completude des etapes d'une session de formation : "statut_completude"
|
|||
- 1 = "Part", Etape partiellement effectué
|
||||
- 0 = "Non", Etape non demarré
|
||||
|
||||
"""
|
||||
"""
|
||||
|
||||
|
||||
"""
|
||||
Gestion du module d'envoie d'email
|
||||
les 'related_collection' sont : ['inscription', 'apprenant', 'contact']
|
||||
"""
|
||||
EMAIL_DEST_RELATED_COLLECTION = ['inscription', 'apprenant', 'contact']
|
3413
Log/log_file.log
3413
Log/log_file.log
File diff suppressed because it is too large
Load Diff
259
email_mgt.py
259
email_mgt.py
|
@ -3004,3 +3004,262 @@ def Strip_Get_Customer_Upcoming_Invoice(diction):
|
|||
exc_type, exc_obj, exc_tb = sys.exc_info()
|
||||
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
||||
return False, " Impossible d'envoyer les informations de la prochaines factures "
|
||||
|
||||
|
||||
"""
|
||||
Cette fonction permet d'envoyer les emails générés par le module font "module_email_management"
|
||||
Pour ce faire, la fonction prend en argument :
|
||||
- list_email_dest
|
||||
- list_email_cc
|
||||
- list_email_bcc
|
||||
- email_corps
|
||||
- related_collection
|
||||
- pièce jointes
|
||||
|
||||
algo :
|
||||
le corps du mail peut porter des tag de replacement comme le nom, prenom.
|
||||
On va aller chercher dans la collection "related_collection" les donnée des personnes qui sont dans le "list_email_dest"
|
||||
|
||||
En gros, si list_email_dest[x] = related_collection.email, alors ok.
|
||||
Si on ne trouve pas la personne, on n'envoie par l'email.
|
||||
/!\ Ceci est une sécurité pour eviter d'envoyer des email des personnes non connus de mysy/
|
||||
|
||||
"""
|
||||
def Prepare_and_Send_Email_From_Front(tab_files, Folder, diction):
|
||||
try:
|
||||
diction = mycommon.strip_dictionary(diction)
|
||||
|
||||
|
||||
field_list = ['token', 'list_email_dest', 'list_email_cc', 'list_email_bcc', 'email_corps', 'related_collection',
|
||||
'email_sujet']
|
||||
incom_keys = diction.keys()
|
||||
|
||||
for val in incom_keys:
|
||||
if val not in field_list and val.startswith('my_') is False:
|
||||
mycommon.myprint(str(inspect.stack()[0][
|
||||
3]) + " - Le champ '" + val + "' n'est pas autorisé")
|
||||
return False, "Impossible de créer le stagiaire. Toutes les informations fournies ne sont pas valables"
|
||||
|
||||
field_list_obligatoire = ['token', 'list_email_dest', 'related_collection',
|
||||
'email_sujet', 'email_corps']
|
||||
|
||||
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, " La valeur '" + val + "' n'est pas presente dans liste"
|
||||
|
||||
my_token = ""
|
||||
if ("token" in diction.keys()):
|
||||
if diction['token']:
|
||||
my_token = diction['token']
|
||||
|
||||
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
||||
if (local_status is not True):
|
||||
return local_status, my_partner
|
||||
|
||||
"""
|
||||
Verifier la validité "related_collection"
|
||||
"""
|
||||
related_collection = str(diction['related_collection']).strip().lower()
|
||||
if(related_collection not in MYSY_GV.EMAIL_DEST_RELATED_COLLECTION ):
|
||||
mycommon.myprint(
|
||||
str(inspect.stack()[0][
|
||||
3]) + " Le champ 'related_collection' est invalide ")
|
||||
return False, " Le champ 'related_collection' est invalide "
|
||||
|
||||
local_partner_owner_recid = "partner_owner_recid"
|
||||
if (related_collection == "contact"):
|
||||
local_partner_owner_recid = "partner_recid"
|
||||
|
||||
|
||||
|
||||
"""
|
||||
Gestion du sujet :
|
||||
On le tronc à 200 caractère max
|
||||
"""
|
||||
email_sujet = ""
|
||||
if(len(str(diction['email_sujet'])) > 200 ):
|
||||
email_sujet = str(diction['email_sujet'])[0:200]
|
||||
else:
|
||||
email_sujet = str(diction['email_sujet'])
|
||||
|
||||
|
||||
"""
|
||||
Verifier que le format list_email_cc est valide
|
||||
"""
|
||||
tab_email_cc = ""
|
||||
if ("list_email_cc" in diction.keys()):
|
||||
if diction['list_email_cc']:
|
||||
tab_email_cc = diction['list_email_cc']
|
||||
|
||||
tab_email_cc = str(tab_email_cc).split(",")
|
||||
for email_cc in tab_email_cc:
|
||||
if (email_cc):
|
||||
if (mycommon.isEmailValide(str(email_cc)) is False):
|
||||
mycommon.myprint(
|
||||
str(inspect.stack()[0][3]) + " L'adresse email en CC '" + str(email_cc) + "' est invalide ")
|
||||
return False, "L'adresse email en CC '" + str(email_cc) + "' est invalide"
|
||||
|
||||
"""
|
||||
Verifier que le format list_email_bcc est valide
|
||||
"""
|
||||
tab_email_bcc = ""
|
||||
if ("list_email_bcc" in diction.keys()):
|
||||
if diction['list_email_bcc']:
|
||||
tab_email_bcc = diction['list_email_bcc']
|
||||
|
||||
|
||||
tab_email_bcc = str(tab_email_bcc).split(",")
|
||||
for email_bcc in tab_email_bcc:
|
||||
if (email_bcc):
|
||||
|
||||
if (mycommon.isEmailValide(str(email_bcc)) is False):
|
||||
mycommon.myprint(
|
||||
str(inspect.stack()[0][3]) + " L'adresse email en BCC '" + str(email_bcc) + "' est invalide ")
|
||||
return False, "L'adresse email en BCC '" + str(email_bcc) + "' est invalide"
|
||||
|
||||
|
||||
"""
|
||||
Verifier la validité des emails de destinataire
|
||||
"""
|
||||
tab_related_collection_id = []
|
||||
tab_email_dest = ""
|
||||
if ("list_email_dest" in diction.keys()):
|
||||
if diction['list_email_dest']:
|
||||
tab_email_dest = diction['list_email_dest']
|
||||
|
||||
tab_email_dest = str(tab_email_dest).split(",")
|
||||
for email_dest in tab_email_dest:
|
||||
if( email_dest ):
|
||||
if (mycommon.isEmailValide(str(email_dest)) is False):
|
||||
mycommon.myprint(
|
||||
str(inspect.stack()[0][3]) + " L'adresse email '" + str(email_dest) + "' est invalide ")
|
||||
return False, "L'adresse email '" + str(email_dest) + "' est invalide"
|
||||
|
||||
|
||||
|
||||
|
||||
is_valide_email_in_related_collection = MYSY_GV.dbname[str(related_collection)].count_documents({"email":str(email_dest),
|
||||
'valide':'1',
|
||||
local_partner_owner_recid:str(my_partner['recid'])
|
||||
})
|
||||
|
||||
if( is_valide_email_in_related_collection < 1 ):
|
||||
mycommon.myprint(
|
||||
str(inspect.stack()[0][
|
||||
3]) + " L'adresse email '" + str(email_dest) + "' est invalide (2) ")
|
||||
return False, " L'adresse email '" + str(email_dest) + "' est invalide (2) "
|
||||
|
||||
is_valide_email_in_related_collection_data = MYSY_GV.dbname[str(related_collection)].find_one(
|
||||
{"email": str(email_dest),
|
||||
'valide': '1',
|
||||
local_partner_owner_recid: str(my_partner['recid'])
|
||||
}, {'_id':1})
|
||||
|
||||
tab_related_collection_id.append(str(is_valide_email_in_related_collection_data['_id']))
|
||||
|
||||
|
||||
#print(" ### la liste _id des destinataire est ", str(tab_related_collection_id))
|
||||
|
||||
# Sauvegarde des fichiers joints depuis le front
|
||||
tab_saved_file_full_path = []
|
||||
for file in tab_files:
|
||||
status, saved_file_full_path = mycommon.Upload_Save_PDF_IMG_File(file, Folder)
|
||||
if (status is False):
|
||||
mycommon.myprint("Impossible de récupérer correctement le fichier à importer")
|
||||
return False, "Impossible de récupérer correctement le fichier à importer"
|
||||
|
||||
tab_saved_file_full_path.append(saved_file_full_path)
|
||||
|
||||
# Recuperation des donnes SMTP
|
||||
local_stpm_status, partner_SMTP_COUNT_smtpsrv, partner_own_smtp_value, partner_SMTP_COUNT_password, partner_SMTP_COUNT_user, partner_SMTP_COUNT_From_User, partner_SMTP_COUNT_port = mycommon.Get_Partner_SMTP_Param(
|
||||
my_partner['recid'])
|
||||
|
||||
if (local_stpm_status is False):
|
||||
return local_stpm_status, partner_own_smtp_value
|
||||
|
||||
smtpserver = None
|
||||
|
||||
if (str(partner_own_smtp_value) == "1"):
|
||||
smtpserver = smtplib.SMTP(partner_SMTP_COUNT_smtpsrv, partner_SMTP_COUNT_port)
|
||||
else:
|
||||
smtpserver = smtplib.SMTP(MYSY_GV.O365_SMTP_COUNT_smtpsrv, MYSY_GV.O365_SMTP_COUNT_port)
|
||||
|
||||
smtpserver.ehlo()
|
||||
smtpserver.starttls()
|
||||
smtpserver.login(MYSY_GV.O365_SMTP_COUNT_user, MYSY_GV.O365_SMTP_COUNT_password)
|
||||
cpt_email_envoye = 0
|
||||
|
||||
"""
|
||||
Envoie proprement-dit des emails
|
||||
"""
|
||||
for user_id in tab_related_collection_id:
|
||||
user_related_collection_data = MYSY_GV.dbname[str(related_collection)].find_one(
|
||||
{"_id": ObjectId(str(user_id)),
|
||||
'valide': '1',
|
||||
local_partner_owner_recid: str(my_partner['recid'])
|
||||
}, {'_id': 1, 'email':1, 'nom':1, 'prenom':1})
|
||||
|
||||
nom=""
|
||||
prenom = ""
|
||||
email = ""
|
||||
|
||||
if( "nom" in user_related_collection_data.keys() ):
|
||||
nom = user_related_collection_data['nom']
|
||||
|
||||
if ("prenom" in user_related_collection_data.keys()):
|
||||
prenom = user_related_collection_data['prenom']
|
||||
|
||||
if ("email" in user_related_collection_data.keys()):
|
||||
email = user_related_collection_data['email']
|
||||
|
||||
node = {}
|
||||
node['nom'] = nom
|
||||
node['prenom'] = prenom
|
||||
body = {
|
||||
"params": node,
|
||||
}
|
||||
|
||||
# Creation de l'email à enoyer
|
||||
msg = MIMEMultipart("alternative")
|
||||
contenu_doc_Template = jinja2.Template(str(diction['email_corps']))
|
||||
|
||||
sourceHtml = contenu_doc_Template.render(params=body["params"])
|
||||
|
||||
html_mime = MIMEText(sourceHtml, 'html')
|
||||
|
||||
if (str(partner_own_smtp_value) == "1"):
|
||||
msg.attach(html_mime)
|
||||
msg['From'] = partner_SMTP_COUNT_From_User
|
||||
msg['Cc'] = ", ".join(tab_email_cc)
|
||||
msg['Bcc'] = ", ".join(tab_email_bcc)
|
||||
msg['Subject'] = email_sujet
|
||||
msg['to'] = str(email)
|
||||
val = smtpserver.send_message(msg)
|
||||
print(" Email a envoyé " + str(email))
|
||||
cpt_email_envoye = cpt_email_envoye + 1
|
||||
|
||||
else:
|
||||
msg.attach(html_mime)
|
||||
msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User
|
||||
msg['Cc'] = ", ".join(tab_email_cc)
|
||||
msg['Bcc'] = ", ".join(tab_email_bcc)
|
||||
msg['Subject'] = email_sujet
|
||||
msg['to'] = str(email)
|
||||
val = smtpserver.send_message(msg)
|
||||
print(" Email a envoyé " + str(email))
|
||||
cpt_email_envoye = cpt_email_envoye + 1
|
||||
|
||||
|
||||
smtpserver.close()
|
||||
|
||||
print(" NB_email envoyé = ", cpt_email_envoye)
|
||||
|
||||
return True, " Les emails ont été correctement envoyés"
|
||||
|
||||
except Exception as e:
|
||||
exc_type, exc_obj, exc_tb = sys.exc_info()
|
||||
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
||||
return False, " Impossible d'envoyer les emails "
|
||||
|
|
21
main.py
21
main.py
|
@ -10528,6 +10528,27 @@ def DeleteImage_Partner_Product():
|
|||
|
||||
|
||||
|
||||
"""
|
||||
API qui permet d'envoyer des emails generés depuis le front
|
||||
"""
|
||||
@app.route('/myclass/api/Prepare_and_Send_Email_From_Front/', methods=['POST','GET'])
|
||||
@crossdomain(origin='*')
|
||||
def Prepare_and_Send_Email_From_Front():
|
||||
# On recupere le corps (payload) de la requete
|
||||
payload = mycommon.strip_dictionary (request.form.to_dict())
|
||||
print(" ### Prepare_and_Send_Email_From_Front payload = ",payload)
|
||||
file = []
|
||||
tab_files = []
|
||||
if request.method == 'POST':
|
||||
# Create variable for uploaded file
|
||||
tab_files = []
|
||||
for tmp in request.files.getlist("File"):
|
||||
tab_files.append(tmp)
|
||||
|
||||
status, retval = emails.Prepare_and_Send_Email_From_Front(tab_files, MYSY_GV.TEMPORARY_DIRECTORY_V2, payload)
|
||||
|
||||
return jsonify(status=status, message=retval)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue