Spaces:
Running
Running
Update src/apps/email_utils.py
Browse files- src/apps/email_utils.py +66 -65
src/apps/email_utils.py
CHANGED
|
@@ -1,65 +1,66 @@
|
|
| 1 |
-
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType
|
| 2 |
-
from pydantic import EmailStr
|
| 3 |
-
import os
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
-
load_dotenv()
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
<
|
| 48 |
-
<p>
|
| 49 |
-
<p>
|
| 50 |
-
<
|
| 51 |
-
<
|
| 52 |
-
<p
|
| 53 |
-
<p style="color: #
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType
|
| 2 |
+
from pydantic import EmailStr
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
BASE_URL = os.getenv("APP_URL", "http://localhost:8000")
|
| 10 |
+
|
| 11 |
+
# Support both SMTP_* and MAIL_* environment variable naming conventions
|
| 12 |
+
MAIL_USERNAME = os.getenv("MAIL_USERNAME") or os.getenv("SMTP_USERNAME", "")
|
| 13 |
+
MAIL_PASSWORD = os.getenv("MAIL_PASSWORD") or os.getenv("SMTP_PASSWORD", "")
|
| 14 |
+
MAIL_FROM = os.getenv("MAIL_FROM") or os.getenv("FROM_EMAIL", "")
|
| 15 |
+
MAIL_PORT = int(os.getenv("MAIL_PORT") or os.getenv("SMTP_PORT", "587"))
|
| 16 |
+
MAIL_SERVER = os.getenv("MAIL_SERVER") or os.getenv("SMTP_SERVER", "smtp.gmail.com")
|
| 17 |
+
|
| 18 |
+
conf = ConnectionConfig(
|
| 19 |
+
MAIL_USERNAME=MAIL_USERNAME,
|
| 20 |
+
MAIL_PASSWORD=MAIL_PASSWORD,
|
| 21 |
+
MAIL_FROM=MAIL_FROM,
|
| 22 |
+
MAIL_PORT=MAIL_PORT,
|
| 23 |
+
MAIL_SERVER=MAIL_SERVER,
|
| 24 |
+
MAIL_STARTTLS=True,
|
| 25 |
+
MAIL_SSL_TLS=False,
|
| 26 |
+
USE_CREDENTIALS=True,
|
| 27 |
+
VALIDATE_CERTS=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
async def send_reset_email(email: EmailStr, token: str):
|
| 31 |
+
# Validate email configuration before attempting to send
|
| 32 |
+
if not MAIL_USERNAME or not MAIL_PASSWORD or not MAIL_FROM:
|
| 33 |
+
missing_vars = []
|
| 34 |
+
if not MAIL_USERNAME:
|
| 35 |
+
missing_vars.append("MAIL_USERNAME or SMTP_USERNAME")
|
| 36 |
+
if not MAIL_PASSWORD:
|
| 37 |
+
missing_vars.append("MAIL_PASSWORD or SMTP_PASSWORD")
|
| 38 |
+
if not MAIL_FROM:
|
| 39 |
+
missing_vars.append("MAIL_FROM or FROM_EMAIL")
|
| 40 |
+
raise ValueError(f"Email configuration missing: {', '.join(missing_vars)}. Please set these environment variables.")
|
| 41 |
+
|
| 42 |
+
reset_link = f"{BASE_URL}/reset-password?token={token}"
|
| 43 |
+
html_body = f"""
|
| 44 |
+
<html>
|
| 45 |
+
<body style="font-family: Arial, sans-serif; padding: 20px; background-color: #f4f4f4;">
|
| 46 |
+
<div style="max-width: 600px; margin: 0 auto; background-color: white; padding: 30px; border-radius: 10px;">
|
| 47 |
+
<h2 style="color: #9b87f5;">Password Reset Request</h2>
|
| 48 |
+
<p>Hello,</p>
|
| 49 |
+
<p>We received a request to reset your password for your Law Bot account.</p>
|
| 50 |
+
<p>Click the button below to reset your password:</p>
|
| 51 |
+
<a href="{reset_link}" style="display: inline-block; padding: 12px 24px; background-color: #9b87f5; color: white; text-decoration: none; border-radius: 6px; margin: 20px 0;">Reset Password</a>
|
| 52 |
+
<p>Or copy and paste this link into your browser:</p>
|
| 53 |
+
<p style="color: #666; word-break: break-all;">{reset_link}</p>
|
| 54 |
+
<p style="color: #999; font-size: 12px; margin-top: 30px;">If you didn't request this, you can safely ignore this email.</p>
|
| 55 |
+
</div>
|
| 56 |
+
</body>
|
| 57 |
+
</html>
|
| 58 |
+
"""
|
| 59 |
+
message = MessageSchema(
|
| 60 |
+
subject="Password Reset Request - Law Bot",
|
| 61 |
+
recipients=[email],
|
| 62 |
+
body=html_body,
|
| 63 |
+
subtype=MessageType.html
|
| 64 |
+
)
|
| 65 |
+
fm = FastMail(conf)
|
| 66 |
+
await fm.send_message(message)
|