Update email_utils.py
Browse files- email_utils.py +68 -50
email_utils.py
CHANGED
|
@@ -1,58 +1,76 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
from email.mime.text import MIMEText
|
| 4 |
-
from email.mime.multipart import MIMEMultipart
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
def send_otp_email(receiver_email, otp):
|
| 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 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
|
|
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Load variables from .env file
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
SENDGRID_API_KEY = os.getenv("SENDGRID_API_KEY")
|
| 19 |
+
SENDER_EMAIL = os.getenv("SENDER_EMAIL")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
|
| 25 |
|
| 26 |
def send_otp_email(receiver_email, otp):
|
| 27 |
+
"""
|
| 28 |
+
Sends an OTP email using SendGrid API v3.
|
| 29 |
+
"""
|
| 30 |
+
url = "https://api.sendgrid.com/v3/mail/send"
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
headers = {
|
| 34 |
+
"Authorization": f"Bearer {SENDGRID_API_KEY}",
|
| 35 |
+
"Content-Type": "application/json"
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
data = {
|
| 40 |
+
"personalizations": [{
|
| 41 |
+
"to": [{"email": receiver_email}]
|
| 42 |
+
}],
|
| 43 |
+
"from": {
|
| 44 |
+
"email": SENDER_EMAIL,
|
| 45 |
+
"name": "FitPlan AI"
|
| 46 |
+
},
|
| 47 |
+
"subject": "Your FitPlan AI Verification Code",
|
| 48 |
+
"content": [{
|
| 49 |
+
"type": "text/plain",
|
| 50 |
+
"value": f"Welcome to FitPlan AI! Your one-time password (OTP) is: {otp}. This code will expire in 1 hour."
|
| 51 |
+
}]
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
response = requests.post(url, headers=headers, json=data)
|
| 61 |
+
|
| 62 |
+
# Log status to terminal for debugging
|
| 63 |
+
print(f"DEBUG: SendGrid Response Status: {response.status_code}")
|
| 64 |
+
|
| 65 |
+
if response.status_code != 202:
|
| 66 |
+
print(f"DEBUG: SendGrid Error Detail: {response.text}")
|
| 67 |
+
raise Exception(f"Failed to send email: {response.text}")
|
| 68 |
+
|
| 69 |
+
return True
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"DEBUG: Exception occurred: {e}")
|
| 73 |
+
raise e
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|