-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_redmail.py
56 lines (49 loc) · 1.81 KB
/
email_redmail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
from dotenv import load_dotenv
from redmail import gmail # type: ignore
import PIL
import streamlit as st
import datetime
import pytz
load_dotenv()
gmail.username = os.getenv('EMAIL_ADDRESS')
gmail.password = os.getenv('EMAIL_PASSWORD')
# https://red-mail.readthedocs.io/en/latest/tutorials/example.html
# https://medium.com/@koli.mikael/how-to-send-emails-via-python-the-better-way-introduction-to-red-mail-cf20c4b604b9
receivers=[os.getenv('EMAIL_ADDRESS')]
prefix = "🚢 Trafic maritime - "
def send_email(email_sender, subject, body):
"""
Envoie un email.
"""
gmail.send(
sender=email_sender,
receivers=receivers,
subject=prefix + "📧 Formulaire de contact : " + subject,
text=body
)
def send_alert_email_zero_detection(job_id, img_plotted):
detection_timezone = pytz.timezone('Europe/Paris')
detection_datetime = datetime.datetime.now(detection_timezone)
date_heure = detection_datetime.strftime("%a %d %b %H:%M")
gmail.send(
subject=prefix + "🚨 Alerte - Faux négatif - Aucune détection",
receivers=receivers,
html="""
<h1>🚨 Alerte - Faux négatif - Aucune détection</h1>
<p>En pièce-jointe, l'image contenant un faux négatif. </p>
<ul>
<li>ID de la tâche de détection : {{ job_id }}</li>
<li>Date et heure de la détection : {{ date_heure }}</li>
<li>Utilisateur : {{ user_name }}</li>
</ul>
{{ my_image }}
""",
body_params={
'job_id': job_id,
'date_heure' : date_heure,
'user_name': st.session_state["name"]
},
body_images={"my_image ": PIL.Image.fromarray(img_plotted)}
)
print(f"""🚨 Email d'alerte "Faux négatif" envoyé ! """)