2020-10-31 13:21:13 +01:00
|
|
|
from email.message import EmailMessage
|
|
|
|
from secrets import token_hex
|
2020-10-31 14:21:00 +01:00
|
|
|
from smtplib import SMTP_SSL
|
2020-11-25 13:44:07 +01:00
|
|
|
from typing import Tuple
|
2020-10-31 14:21:00 +01:00
|
|
|
|
|
|
|
from constants import DOMAIN, PASSWORD, USERNAME
|
2020-11-25 11:06:38 +01:00
|
|
|
from database.crud import save_attribute
|
2020-10-31 13:21:13 +01:00
|
|
|
|
|
|
|
|
2020-11-25 13:44:07 +01:00
|
|
|
def initialize_smtp(domain, username, password) -> SMTP_SSL:
|
|
|
|
"""
|
|
|
|
Creates the SMTP connection to the mail server
|
|
|
|
"""
|
2020-10-31 13:21:13 +01:00
|
|
|
server = SMTP_SSL(domain)
|
|
|
|
server.login(username, password)
|
|
|
|
return server
|
|
|
|
|
|
|
|
|
2020-11-25 13:44:07 +01:00
|
|
|
def format_message(sender, recipient) -> Tuple[EmailMessage, str]:
|
|
|
|
"""
|
|
|
|
Creates the email message and formats it accordingly
|
|
|
|
"""
|
2020-11-25 11:06:38 +01:00
|
|
|
code = token_hex(4)
|
|
|
|
body = "La clave es {}. Envíele esta clave al bot.".format()
|
2020-10-31 13:21:13 +01:00
|
|
|
message = EmailMessage()
|
|
|
|
message["From"] = sender
|
|
|
|
message["To"] = recipient
|
|
|
|
message["Subject"] = "Clave para verificar su identidad"
|
|
|
|
message.set_content(body)
|
2020-11-25 11:06:38 +01:00
|
|
|
return message, code
|
2020-10-31 13:21:13 +01:00
|
|
|
|
|
|
|
|
2020-11-25 13:44:07 +01:00
|
|
|
def send_mail(recipient) -> None:
|
|
|
|
"""
|
|
|
|
Sends an email to the specified recipient
|
|
|
|
"""
|
2020-10-31 13:21:13 +01:00
|
|
|
server = initialize_smtp(domain=DOMAIN, username=USERNAME, password=PASSWORD)
|
2020-11-25 11:06:38 +01:00
|
|
|
message, code = format_message(sender=USERNAME, recipient=recipient)
|
|
|
|
save_attribute(attribute="codigo", data=code)
|
2020-10-31 13:21:13 +01:00
|
|
|
server.send_message(msg=message)
|
|
|
|
server.close()
|