Skip to content

Commit

Permalink
use aiosmtplib
Browse files Browse the repository at this point in the history
  • Loading branch information
dmkjfs committed Dec 20, 2024
1 parent 0ab30ca commit 3c61cf5
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/infrastructure/email.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")

0 comments on commit 3c61cf5

Please sign in to comment.