Email notification

This commit is contained in:
James Pattinson
2025-10-25 13:31:03 +00:00
parent 91e820b9a8
commit d5f05941c9
9 changed files with 880 additions and 3 deletions

48
backend/app/core/email.py Normal file
View File

@@ -0,0 +1,48 @@
import aiosmtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from jinja2 import Environment, FileSystemLoader
import os
from app.core.config import settings
class EmailService:
def __init__(self):
self.smtp_host = settings.mail_host
self.smtp_port = settings.mail_port
self.smtp_user = settings.mail_username
self.smtp_password = settings.mail_password
self.from_email = settings.mail_from
self.from_name = settings.mail_from_name
# Set up Jinja2 environment for templates
template_dir = os.path.join(os.path.dirname(__file__), '..', 'templates')
self.jinja_env = Environment(loader=FileSystemLoader(template_dir))
async def send_email(self, to_email: str, subject: str, template_name: str, template_vars: dict):
# Render the template
template = self.jinja_env.get_template(template_name)
html_content = template.render(**template_vars)
# Create message
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = f"{self.from_name} <{self.from_email}>"
msg['To'] = to_email
# Attach HTML content
html_part = MIMEText(html_content, 'html')
msg.attach(html_part)
# Send email
try:
async with aiosmtplib.SMTP(hostname=self.smtp_host, port=self.smtp_port, use_tls=True) as smtp:
await smtp.login(self.smtp_user, self.smtp_password)
await smtp.send_message(msg)
except Exception as e:
# Log error, but for now, print
print(f"Failed to send email: {e}")
# In production, use logging
email_service = EmailService()