1
0
mirror of https://gitlab.com/akasroua/covot synced 2025-12-24 16:11:55 +01:00

Document notifier and crud modules

This commit is contained in:
2020-11-25 13:44:07 +01:00
parent 23a6e29ff9
commit 2d6597988c
2 changed files with 34 additions and 8 deletions

View File

@@ -1,18 +1,25 @@
from email.message import EmailMessage
from secrets import token_hex
from smtplib import SMTP_SSL
from typing import Tuple
from constants import DOMAIN, PASSWORD, USERNAME
from database.crud import save_attribute
def initialize_smtp(domain, username, password):
def initialize_smtp(domain, username, password) -> SMTP_SSL:
"""
Creates the SMTP connection to the mail server
"""
server = SMTP_SSL(domain)
server.login(username, password)
return server
def format_message(sender, recipient):
def format_message(sender, recipient) -> Tuple[EmailMessage, str]:
"""
Creates the email message and formats it accordingly
"""
code = token_hex(4)
body = "La clave es {}. Envíele esta clave al bot.".format()
message = EmailMessage()
@@ -23,7 +30,10 @@ def format_message(sender, recipient):
return message, code
def send_mail(recipient):
def send_mail(recipient) -> None:
"""
Sends an email to the specified recipient
"""
server = initialize_smtp(domain=DOMAIN, username=USERNAME, password=PASSWORD)
message, code = format_message(sender=USERNAME, recipient=recipient)
save_attribute(attribute="codigo", data=code)