Shrey0405 commited on
Commit
4d57727
·
verified ·
1 Parent(s): 1d0c076

Update email_utils.py

Browse files
Files changed (1) hide show
  1. email_utils.py +68 -50
email_utils.py CHANGED
@@ -1,58 +1,76 @@
1
  import os
2
- import smtplib
3
- from email.mime.text import MIMEText
4
- from email.mime.multipart import MIMEMultipart
5
  from dotenv import load_dotenv
6
 
7
- # Load environment variables (Fallback for local testing)
 
 
 
 
 
8
  load_dotenv()
9
 
10
- EMAIL_USER = os.getenv("EMAIL_USER")
11
- EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
 
 
 
 
 
 
 
 
 
12
 
13
 
14
  def send_otp_email(receiver_email, otp):
15
- subject = "Verify Your Login"
16
-
17
- body = f"""
18
- <html>
19
- <body style="font-family: Arial, sans-serif;">
20
- <h2>FitPlan AI Verification</h2>
21
- <p>Your OTP is:</p>
22
- <h1 style="color: #6366f1; font-size: 2.5rem; letter-spacing: 2px;">{otp}</h1>
23
- <p>This OTP expires in 1 hour.</p>
24
- </body>
25
- </html>
26
- """
27
-
28
- msg = MIMEMultipart()
29
- msg["From"] = EMAIL_USER
30
- msg["To"] = receiver_email
31
- msg["Subject"] = subject
32
-
33
- msg.attach(MIMEText(body, "html"))
34
-
35
- try:
36
- print(f"DEBUG LOG: Attempting secure SSL connection to port 465. EMAIL_USER is loaded: {EMAIL_USER is not None}")
37
-
38
- # Switch to SMTP_SSL and use port 465 to bypass firewall blocks
39
- server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
40
-
41
- server.login(
42
- EMAIL_USER,
43
- EMAIL_PASSWORD
44
- )
45
-
46
- server.send_message(msg)
47
- server.quit()
48
-
49
- print("DEBUG: Email sent successfully via SSL")
50
- return True
51
-
52
- except Exception as e:
53
- print("FULL ERROR LOGGED:")
54
- print(e)
55
- # This will force the real error message to pop up right on your Streamlit screen!
56
- import streamlit as st
57
- st.error(f"SYSTEM SMTP ERROR: {str(e)}")
58
- return False
 
 
 
 
 
 
 
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
+