forked from m-grzesiak/yalma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_sender.py
41 lines (28 loc) · 1.31 KB
/
email_sender.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
import smtplib
from email.mime.text import MIMEText
import config_loader
_EMAIL_SUBJECT = "[YALMA] Visits are available"
class EmailSenderException(Exception):
pass
def __load_email_setting():
settings = config_loader.read_configuration("email_settings", ["username", "password", "smtp_server", "smtp_port"])
return settings["username"], settings["password"], settings["smtp_server"], settings["smtp_port"]
def send_email(to: str, message: str):
username, password, smtp_server, smtp_port = __load_email_setting()
email_message = MIMEText(message)
email_message["From"] = username
email_message["To"] = to
email_message["Subject"] = _EMAIL_SUBJECT
mail_server_connection = None
try:
print("Sending an email message with notification...")
mail_server_connection = smtplib.SMTP_SSL(smtp_server, smtp_port)
mail_server_connection.ehlo()
mail_server_connection.login(username, password)
mail_server_connection.sendmail(username, to, email_message.as_string())
print("The notification has been successfully sent")
except Exception as exception:
raise EmailSenderException(f"Unable to send the email. Details: {exception}") from None
finally:
if mail_server_connection is not None:
mail_server_connection.close()