Files
Aether/backend/app/utils/email_client.py
T
2026-02-06 19:50:48 +03:00

44 lines
1.5 KiB
Python

import smtplib
import logging
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from jinja2 import Environment, FileSystemLoader
from app.core.config import settings
log = logging.getLogger(__name__)
class EmailClient:
env = Environment(
loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "..", "templates"))
)
@classmethod
def render(cls, template_path, **kwargs):
log.debug(f"Rendering {template_path}", extra={"kwargs": kwargs, "template_path": template_path})
template = cls.env.get_template(template_path)
return template.render(**kwargs)
@classmethod
def send_email(cls, to: str, subject: str, html: str, body: str):
log.info("Sending email", extra={"subject": subject, "to": to})
try:
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = settings.SMTP_EMAIL
msg["To"] = to
msg.attach(MIMEText(html, "html", "utf-8"))
msg.attach(MIMEText(body, "plain", "utf-8"))
with smtplib.SMTP_SSL(settings.SMTP_SERVER, settings.SMTP_PORT) as smtp:
smtp.login(settings.SMTP_EMAIL, settings.SMTP_PASS)
smtp.send_message(msg)
log.info("Email sent successfully %s", to, extra={"to": to, "subject": subject})
except Exception as e:
log.error(f"Failed to send email: {str(e)}", extra={"to": to, "subject": subject})
raise e