Springboardmen commited on
Commit
eae1ad4
·
verified ·
1 Parent(s): fedc971

Update email_utils.py

Browse files
Files changed (1) hide show
  1. email_utils.py +23 -20
email_utils.py CHANGED
@@ -1,27 +1,30 @@
1
  import os
2
- from dotenv import load_dotenv
3
- import smtplib
4
- from email.mime.text import MIMEText
5
-
6
- # 🔹 LOAD ENV FILE HERE
7
- load_dotenv()
8
 
 
9
  SENDER_EMAIL = os.getenv("SENDER_EMAIL")
10
- SENDER_PASSWORD = os.getenv("SENDER_PASSWORD")
11
 
12
  def send_otp_email(receiver_email, otp):
13
- subject = "Your FitPlan AI OTP Code"
14
- body = f"""
15
- Your OTP is: {otp}
16
- Valid for 5 minutes.
17
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- msg = MIMEText(body)
20
- msg["Subject"] = subject
21
- msg["From"] = SENDER_EMAIL
22
- msg["To"] = receiver_email
23
 
24
- with smtplib.SMTP("smtp.gmail.com", 587) as server:
25
- server.starttls()
26
- server.login(SENDER_EMAIL, SENDER_PASSWORD)
27
- server.send_message(msg)
 
1
  import os
2
+ import requests
 
 
 
 
 
3
 
4
+ SENDGRID_API_KEY = os.getenv("SENDGRID_API_KEY")
5
  SENDER_EMAIL = os.getenv("SENDER_EMAIL")
 
6
 
7
  def send_otp_email(receiver_email, otp):
8
+ url = "https://api.sendgrid.com/v3/mail/send"
9
+
10
+ headers = {
11
+ "Authorization": f"Bearer {SENDGRID_API_KEY}",
12
+ "Content-Type": "application/json"
13
+ }
14
+
15
+ data = {
16
+ "personalizations": [{
17
+ "to": [{"email": receiver_email}]
18
+ }],
19
+ "from": {"email": SENDER_EMAIL},
20
+ "subject": "Your FitPlan AI OTP",
21
+ "content": [{
22
+ "type": "text/plain",
23
+ "value": f"Your OTP is: {otp}"
24
+ }]
25
+ }
26
 
27
+ response = requests.post(url, headers=headers, json=data)
 
 
 
28
 
29
+ if response.status_code != 202:
30
+ raise Exception("Failed to send email")