-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.py
45 lines (36 loc) · 1.24 KB
/
mailer.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
import logging
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
logger = logging.getLogger(__name__)
EMAIL_USER = ""
EMAIL_PASS = ""
EMAIL_RECIPIENTS = [
"giokosmopo@upatras.gr",
"akaza@upatras.gr",
"sylgo2thermi@gmail.com",
"mail@2dim-therm.thess.sch.gr",
"grigoriadoumaria100@gmail.com",
"e.adamopoulou@thermi.gov.gr",
]
SUBJECT = "Δίκτυο αιωρούμενων σωματιδίων Δήμου Θέρμης"
dirname = os.path.dirname(__file__)
mail_template = os.path.join(dirname, "mail_template.html")
with open(mail_template, "r", encoding="utf-8") as f:
BODY = f.read()
def send_mail(html_table):
"""Sends mail to multiple recipients if necessary"""
toaddr = ", ".join(EMAIL_RECIPIENTS)
msg = MIMEMultipart()
msg["From"] = EMAIL_USER
msg["To"] = toaddr
msg["Subject"] = SUBJECT
msg.attach(MIMEText(BODY.format(html_table=html_table), "html"))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(EMAIL_USER, EMAIL_PASS)
text = msg.as_string()
server.sendmail(EMAIL_USER, EMAIL_RECIPIENTS, text)
server.quit()
logger.info(f"Mail sent")