Spaces:
Running
Running
Update src/apps/email_utils.py
Browse files- src/apps/email_utils.py +38 -31
src/apps/email_utils.py
CHANGED
|
@@ -1,28 +1,24 @@
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
-
import
|
|
|
|
| 4 |
|
| 5 |
load_dotenv()
|
| 6 |
|
| 7 |
# Consistent URL usage for deployment
|
| 8 |
-
# Defaults to your LIVE URL so mobile links work
|
| 9 |
BASE_URL = os.getenv("APP_URL", "https://legal-assistant-lawbot.hf.space")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# Initialize Resend
|
| 16 |
-
if RESEND_API_KEY:
|
| 17 |
-
resend.api_key = RESEND_API_KEY
|
| 18 |
|
| 19 |
async def send_reset_email(email: str, token: str):
|
| 20 |
-
"""Send password reset email using
|
| 21 |
-
|
| 22 |
-
# Validate configuration
|
| 23 |
-
if not RESEND_API_KEY:
|
| 24 |
-
raise ValueError("RESEND_API_KEY missing. Hugging Face blocks SMTP, so we must use Resend.")
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
reset_link = f"{BASE_URL}/reset-password?token={token}"
|
| 27 |
|
| 28 |
html_body = f"""
|
|
@@ -47,22 +43,33 @@ async def send_reset_email(email: str, token: str):
|
|
| 47 |
</html>
|
| 48 |
"""
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
print(f"✅ Password reset email sent successfully via Resend to {email}")
|
| 63 |
-
return response
|
| 64 |
-
|
| 65 |
except Exception as e:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
raise ValueError(error_msg)
|
|
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
+
import httpx
|
| 4 |
+
import asyncio
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
# Consistent URL usage for deployment
|
|
|
|
| 9 |
BASE_URL = os.getenv("APP_URL", "https://legal-assistant-lawbot.hf.space")
|
| 10 |
|
| 11 |
+
# Brevo (Sendinblue) Configuration
|
| 12 |
+
BREVO_API_KEY = os.getenv("BREVO_API_KEY", "")
|
| 13 |
+
# Must be your verified sender in Brevo
|
| 14 |
+
BREVO_SENDER_EMAIL = os.getenv("BREVO_SENDER_EMAIL", "vishwaroman04@gmail.com")
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
async def send_reset_email(email: str, token: str):
|
| 17 |
+
"""Send password reset email using Brevo (Sendinblue) API"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
if not BREVO_API_KEY:
|
| 20 |
+
raise ValueError("BREVO_API_KEY missing. Please add it to Secrets.")
|
| 21 |
+
|
| 22 |
reset_link = f"{BASE_URL}/reset-password?token={token}"
|
| 23 |
|
| 24 |
html_body = f"""
|
|
|
|
| 43 |
</html>
|
| 44 |
"""
|
| 45 |
|
| 46 |
+
url = "https://api.brevo.com/v3/smtp/email"
|
| 47 |
+
|
| 48 |
+
payload = {
|
| 49 |
+
"sender": {"name": "Law Bot India", "email": BREVO_SENDER_EMAIL},
|
| 50 |
+
"to": [{"email": email}],
|
| 51 |
+
"subject": "Password Reset Request - Law Bot",
|
| 52 |
+
"htmlContent": html_body
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
headers = {
|
| 56 |
+
"accept": "application/json",
|
| 57 |
+
"api-key": BREVO_API_KEY,
|
| 58 |
+
"content-type": "application/json"
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
try:
|
| 62 |
+
async with httpx.AsyncClient() as client:
|
| 63 |
+
response = await client.post(url, json=payload, headers=headers, timeout=10.0)
|
| 64 |
+
|
| 65 |
+
if response.status_code in [200, 201, 202]:
|
| 66 |
+
print(f"✅ Email sent successfully via Brevo to {email}")
|
| 67 |
+
return True
|
| 68 |
+
else:
|
| 69 |
+
error_msg = f"Brevo API Error: {response.status_code} - {response.text}"
|
| 70 |
+
print(f"❌ {error_msg}")
|
| 71 |
+
raise ValueError(error_msg)
|
| 72 |
+
|
|
|
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
+
print(f"❌ Failed to send via Brevo: {e}")
|
| 75 |
+
raise ValueError(f"Failed to send email: {str(e)}")
|
|
|