From 1504ccc3fe7a68cd596eab654e43bd694d0aa880 Mon Sep 17 00:00:00 2001 From: coolneng Date: Sat, 31 Oct 2020 13:21:13 +0100 Subject: [PATCH] Add email notifier --- src/notifier.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/notifier.py diff --git a/src/notifier.py b/src/notifier.py new file mode 100644 index 0000000..c66a043 --- /dev/null +++ b/src/notifier.py @@ -0,0 +1,27 @@ +from email.message import EmailMessage +from smtplib import SMTP_SSL +from constants import DOMAIN, USERNAME, PASSWORD +from secrets import token_hex + + +def initialize_smtp(domain, username, password): + server = SMTP_SSL(domain) + server.login(username, password) + return server + + +def format_message(sender, recipient): + body = "La clave es {}. EnvĂ­ele esta clave al bot.".format(token_hex(4)) + message = EmailMessage() + message["From"] = sender + message["To"] = recipient + message["Subject"] = "Clave para verificar su identidad" + message.set_content(body) + return message + + +def send_mail(recipient): + server = initialize_smtp(domain=DOMAIN, username=USERNAME, password=PASSWORD) + message = format_message(sender=USERNAME, recipient=recipient) + server.send_message(msg=message) + server.close()