Add email notifier

This commit is contained in:
coolneng 2020-10-31 13:21:13 +01:00
parent da0c65bc7d
commit 1504ccc3fe
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 27 additions and 0 deletions

27
src/notifier.py Normal file
View File

@ -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()