From 3c61cf56334cc8e82f50e9751c42daf2f27af312 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Fri, 20 Dec 2024 20:35:16 +0300 Subject: [PATCH] use aiosmtplib --- src/infrastructure/email.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/infrastructure/email.py b/src/infrastructure/email.py index f053fa6..0d2e587 100644 --- a/src/infrastructure/email.py +++ b/src/infrastructure/email.py @@ -1,22 +1,23 @@ -import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +import aiosmtplib + from src.infrastructure.config import settings from src.infrastructure.loggers import infrastructure as logger -def send_email(message: str, recipient: str, subject: str) -> None: +async def send_email(message: str, recipient: str, subject: str) -> None: sender = settings.email_address password = settings.email_password - smtp_server = settings.smtp_host + smtp_host = settings.smtp_host smtp_port = settings.smtp_port try: - server = smtplib.SMTP(smtp_server, smtp_port) - server.starttls() - server.login(sender, password) + server = aiosmtplib.SMTP(hostname=smtp_host, port=smtp_port) + await server.starttls() + await server.login(sender, password) msg = MIMEMultipart() msg["From"] = sender @@ -26,6 +27,6 @@ def send_email(message: str, recipient: str, subject: str) -> None: body = message msg.attach(MIMEText(body, "plain")) - server.sendmail(sender, recipient, msg.as_string()) + await server.sendmail(sender, recipient, msg.as_string()) except Exception as ex: logger.error(f"{ex}, Maybe, incorrect login or password")