Spaces:
Running
Running
Commit ·
08af537
1
Parent(s): 402ef00
Add safe email utils (no secrets)
Browse files- app/utils/email_utils.py +87 -0
app/utils/email_utils.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
| 7 |
+
DOTENV_PATH = BASE_DIR / ".env"
|
| 8 |
+
|
| 9 |
+
# Load the .env file from the root directory
|
| 10 |
+
load_dotenv(dotenv_path=DOTENV_PATH)
|
| 11 |
+
|
| 12 |
+
BREVO_API_KEY = os.getenv("BREVO_API_KEY")
|
| 13 |
+
|
| 14 |
+
def send_email(to_email: str, subject: str, body: str) -> dict:
|
| 15 |
+
"""
|
| 16 |
+
Sends an email using the Brevo (Sendinblue) API.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
to_email: The recipient's email address.
|
| 20 |
+
subject: The subject line of the email.
|
| 21 |
+
body: The HTML content body of the email.
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
A dictionary containing the HTTP status code and the Brevo API JSON response.
|
| 25 |
+
"""
|
| 26 |
+
if not BREVO_API_KEY:
|
| 27 |
+
print("ERROR: BREVO_API_KEY is not set.")
|
| 28 |
+
return {"status_code": 500, "response": {"message": "API Key missing"}}
|
| 29 |
+
|
| 30 |
+
url = "https://api.brevo.com/v3/smtp/email"
|
| 31 |
+
|
| 32 |
+
payload = {
|
| 33 |
+
"sender": {"name": "FamFin App", "email": "famfinapp579@gmail.com"},
|
| 34 |
+
"to": [{"email": to_email}],
|
| 35 |
+
"subject": subject,
|
| 36 |
+
"htmlContent": f"<p>{body}</p>"
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
headers = {
|
| 40 |
+
"accept": "application/json",
|
| 41 |
+
"api-key": BREVO_API_KEY,
|
| 42 |
+
"content-type": "application/json"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
| 47 |
+
|
| 48 |
+
# --- CRITICAL TROUBLESHOOTING OUTPUT ---
|
| 49 |
+
print("-" * 50)
|
| 50 |
+
print(f"Brevo API Request Status Code: {response.status_code}")
|
| 51 |
+
# ---------------------------------------
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
response_json = response.json()
|
| 55 |
+
if response.status_code not in (200, 201):
|
| 56 |
+
# Log the error details received from Brevo
|
| 57 |
+
print(f"Brevo API Error Response: {response_json}")
|
| 58 |
+
|
| 59 |
+
except requests.exceptions.JSONDecodeError:
|
| 60 |
+
# Handle cases where Brevo returns a non-JSON error (rare)
|
| 61 |
+
response_json = {"message": response.text}
|
| 62 |
+
if response.status_code not in (200, 201):
|
| 63 |
+
print(f"Brevo API Non-JSON Error: {response.text}")
|
| 64 |
+
|
| 65 |
+
return {
|
| 66 |
+
"status_code": response.status_code,
|
| 67 |
+
"response": response_json
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
except requests.exceptions.RequestException as e:
|
| 71 |
+
print(f"ERROR: Failed to connect to Brevo API: {e}")
|
| 72 |
+
return {"status_code": 503, "response": {"message": f"Connection error: {e}"}}
|
| 73 |
+
|
| 74 |
+
# --- Example Usage (Add this to your main script for testing) ---
|
| 75 |
+
# if __name__ == "__main__":
|
| 76 |
+
# # NOTE: Replace with a real email for testing
|
| 77 |
+
# test_result = send_email(
|
| 78 |
+
# to_email="test@example.com",
|
| 79 |
+
# subject="Test Verification Code",
|
| 80 |
+
# body="Your code is 123456"
|
| 81 |
+
# )
|
| 82 |
+
# print("-" * 50)
|
| 83 |
+
# print(f"Final Result: {test_result}")
|
| 84 |
+
# if test_result.get('status_code') == 201:
|
| 85 |
+
# print("SUCCESS: Email request accepted by Brevo.")
|
| 86 |
+
# else:
|
| 87 |
+
# print("FAILURE: Check Brevo logs and API Key.")
|