| import os |
| import requests |
| from dotenv import load_dotenv |
|
|
|
|
| |
| load_dotenv() |
|
|
|
|
| SENDGRID_API_KEY = os.getenv("SENDGRID_API_KEY") |
| SENDER_EMAIL = os.getenv("SENDER_EMAIL") |
|
|
|
|
| def send_otp_email(receiver_email, otp): |
| """ |
| Sends an OTP email using SendGrid API v3. |
| """ |
| url = "https://api.sendgrid.com/v3/mail/send" |
| |
| headers = { |
| "Authorization": f"Bearer {SENDGRID_API_KEY}", |
| "Content-Type": "application/json" |
| } |
| |
| data = { |
| "personalizations": [{ |
| "to": [{"email": receiver_email}] |
| }], |
| "from": { |
| "email": SENDER_EMAIL, |
| "name": "FitPlan AI" |
| }, |
| "subject": "Your FitPlan AI Verification Code", |
| "content": [{ |
| "type": "text/plain", |
| "value": f"Welcome to FitPlan AI! Your one-time password (OTP) is: {otp}. This code will expire in 1 hour." |
| }] |
| } |
|
|
|
|
| try: |
| response = requests.post(url, headers=headers, json=data) |
| |
| |
| print(f"DEBUG: SendGrid Response Status: {response.status_code}") |
| |
| if response.status_code != 202: |
| print(f"DEBUG: SendGrid Error Detail: {response.text}") |
| raise Exception(f"Failed to send email: {response.text}") |
| |
| return True |
| |
| except Exception as e: |
| print(f"DEBUG: Exception occurred: {e}") |
| raise e |
|
|
|
|