Initial commit
This commit is contained in:
46
src/infrastructure/mail/sender.py
Normal file
46
src/infrastructure/mail/sender.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import aiosmtplib
|
||||
from email.message import EmailMessage
|
||||
from src.application.contracts.i_sender import ISender
|
||||
|
||||
|
||||
class EmailSender(ISender):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
host: str,
|
||||
port: int,
|
||||
username: str,
|
||||
password: str,
|
||||
from_addr: str,
|
||||
use_tls: bool = True,
|
||||
timeout: int = 10,
|
||||
):
|
||||
self._host = host
|
||||
self._port = port
|
||||
self._username = username
|
||||
self._password = password
|
||||
self._from_addr = from_addr
|
||||
self._use_tls = use_tls
|
||||
self._timeout = timeout
|
||||
|
||||
async def send(self, to: str, subject: str, body: str, plain: str | None = None) -> None:
|
||||
message = EmailMessage()
|
||||
message["From"] = self._from_addr
|
||||
message["To"] = to
|
||||
message["Subject"] = subject
|
||||
|
||||
if plain:
|
||||
message.set_content(plain)
|
||||
message.add_alternative(body, subtype="html")
|
||||
else:
|
||||
message.set_content(body, subtype="html")
|
||||
|
||||
await aiosmtplib.send(
|
||||
message,
|
||||
hostname=self._host,
|
||||
port=self._port,
|
||||
username=self._username,
|
||||
password=self._password,
|
||||
use_tls=True,
|
||||
timeout=self._timeout,
|
||||
)
|
||||
Reference in New Issue
Block a user