webcodelab commited on
Commit
ee949c5
·
verified ·
1 Parent(s): a6c54e5

Update app/email_utils.py

Browse files
Files changed (1) hide show
  1. app/email_utils.py +28 -20
app/email_utils.py CHANGED
@@ -1,19 +1,18 @@
1
- import smtplib
2
- from email.mime.text import MIMEText
3
- from email.mime.multipart import MIMEMultipart
4
  import os
 
5
 
6
  def send_verification_email(to_email, username, token):
7
- from_email = os.getenv("EMAIL_HOST_USER")
8
- app_password = os.getenv("EMAIL_HOST_PASSWORD")
9
 
10
- if not from_email or not app_password:
11
- error_msg = f"Missing email env vars. EMAIL_HOST_USER={from_email}, EMAIL_HOST_PASSWORD={'SET' if app_password else 'MISSING'}"
12
- print("", error_msg)
13
  return {"error": error_msg}
14
 
15
  subject = "Verify your account"
16
  verification_link = f"https://webcodelab-ai-resume-ranker-api.hf.space/verify/{token}"
 
17
  body = f"""
18
  Hi {username},
19
 
@@ -24,19 +23,28 @@ def send_verification_email(to_email, username, token):
24
  If you didn't sign up, you can ignore this email.
25
  """
26
 
27
- msg = MIMEMultipart()
28
- msg['From'] = from_email
29
- msg['To'] = to_email
30
- msg['Subject'] = subject
31
- msg.attach(MIMEText(body, 'plain'))
 
32
 
33
  try:
34
- with smtplib.SMTP('smtp.gmail.com', 587) as server:
35
- server.starttls()
36
- server.login(from_email, app_password)
37
- server.send_message(msg)
38
- print(f" Email sent to {to_email}")
39
- return True
 
 
 
 
 
 
 
 
40
  except Exception as e:
41
- print("❌ Email error:", str(e))
42
  return {"error": str(e)}
 
 
 
 
1
  import os
2
+ import requests
3
 
4
  def send_verification_email(to_email, username, token):
5
+ api_key = os.getenv("RESEND_API_KEY")
6
+ from_email = os.getenv("RESEND_SENDER")
7
 
8
+ if not api_key or not from_email:
9
+ error_msg = f"Missing email config. RESEND_API_KEY={'SET' if api_key else 'MISSING'}, RESEND_SENDER={from_email}"
10
+ print("❌", error_msg)
11
  return {"error": error_msg}
12
 
13
  subject = "Verify your account"
14
  verification_link = f"https://webcodelab-ai-resume-ranker-api.hf.space/verify/{token}"
15
+
16
  body = f"""
17
  Hi {username},
18
 
 
23
  If you didn't sign up, you can ignore this email.
24
  """
25
 
26
+ data = {
27
+ "from": f"WebCodeLab <{from_email}>",
28
+ "to": [to_email],
29
+ "subject": subject,
30
+ "text": body
31
+ }
32
 
33
  try:
34
+ response = requests.post(
35
+ "https://api.resend.com/emails",
36
+ headers={
37
+ "Authorization": f"Bearer {api_key}",
38
+ "Content-Type": "application/json"
39
+ },
40
+ json=data
41
+ )
42
+ if response.status_code == 200:
43
+ print(f" Email sent to {to_email}")
44
+ return True
45
+ else:
46
+ print(" Resend API error:", response.status_code, response.text)
47
+ return {"error": response.text}
48
  except Exception as e:
49
+ print(" Unexpected error:", str(e))
50
  return {"error": str(e)}