Mobii23 commited on
Commit
ef50d95
·
verified ·
1 Parent(s): f05338f

Update backend/email_utils.py

Browse files
Files changed (1) hide show
  1. backend/email_utils.py +58 -41
backend/email_utils.py CHANGED
@@ -1,59 +1,64 @@
1
  # File: backend/email_utils.py
2
- from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType
3
  from pydantic import EmailStr
4
  import random
5
  import string
 
6
 
7
  # --- CONFIGURATION ---
8
- # Set to True to bypass Hugging Face email blocks and print OTPs to logs
9
- SUPPRESS_SEND = True
 
10
 
11
- conf = ConnectionConfig(
12
- MAIL_USERNAME="duashehzad969@gmail.com",
13
- MAIL_PASSWORD="xyvo lttj sjki cmtx",
14
- MAIL_FROM="duashehzad969@gmail.com",
15
- MAIL_PORT=587,
16
- MAIL_SERVER="smtp.gmail.com",
17
- MAIL_STARTTLS=True,
18
- MAIL_SSL_TLS=False,
19
- USE_CREDENTIALS=True,
20
- VALIDATE_CERTS=True
21
- )
22
 
23
  def generate_otp(length=6):
24
  return ''.join(random.choices(string.digits, k=length))
25
 
26
  async def send_otp_email(email: EmailStr, otp: str, subject="DocuSort Verification"):
27
- if SUPPRESS_SEND:
 
28
  print(f"\n{'='*40}")
29
- print(f" [MOCK EMAIL] To: {email}")
30
- print(f" [MOCK EMAIL] Subject: {subject}")
31
- print(f" [MOCK EMAIL] CODE: {otp}")
32
  print(f"{'='*40}\n")
33
  return
34
 
35
- message = MessageSchema(
36
- subject=subject,
37
- recipients=[email],
38
- body=f"<p>Your verification code is: <strong>{otp}</strong></p><p>This code expires in 5 minutes.</p>",
39
- subtype=MessageType.html
40
- )
 
 
 
 
 
41
 
42
- fm = FastMail(conf)
43
- await fm.send_message(message)
 
 
 
 
 
 
 
 
44
 
45
- # --- PROFESSIONAL SYSTEM ALERTS ---
46
  async def send_alert_email(email: EmailStr, subject: str, body_content: str):
47
- if SUPPRESS_SEND:
48
- print(f"\n{'='*40}")
49
- print(f" [MOCK ALERT] To: {email}")
50
- print(f" [MOCK ALERT] Subject: {subject}")
51
- # Strip HTML tags just for cleaner terminal logging
52
  clean_body = body_content.replace("<br>", "\n").replace("<b style='font-size:24px;'>", "").replace("</b>", "")
53
- print(f" [MOCK ALERT] Body: \n{clean_body}")
 
 
54
  print(f"{'='*40}\n")
55
  return
56
 
 
57
  html_body = f"""
58
  <div style="font-family: Arial, sans-serif; padding: 20px; color: #333; max-width: 600px; margin: 0 auto; border: 1px solid #e2e8f0; border-radius: 8px;">
59
  <h2 style="color: #4f46e5; border-bottom: 2px solid #e0e7ff; padding-bottom: 10px;">DocuSort Notification</h2>
@@ -64,12 +69,24 @@ async def send_alert_email(email: EmailStr, subject: str, body_content: str):
64
  </div>
65
  """
66
 
67
- message = MessageSchema(
68
- subject=subject,
69
- recipients=[email],
70
- body=html_body,
71
- subtype=MessageType.html
72
- )
 
 
 
 
73
 
74
- fm = FastMail(conf)
75
- await fm.send_message(message)
 
 
 
 
 
 
 
 
 
1
  # File: backend/email_utils.py
2
+ import httpx
3
  from pydantic import EmailStr
4
  import random
5
  import string
6
+ import os
7
 
8
  # --- CONFIGURATION ---
9
+ # Securely pull both keys from Hugging Face Settings
10
+ MAILJET_API_KEY = os.getenv("MAILJET_API_KEY")
11
+ MAILJET_SECRET_KEY = os.getenv("MAILJET_SECRET_KEY")
12
 
13
+ # This MUST exactly match the email you registered in Mailjet
14
+ SENDER_EMAIL = "docusortofficial@gmail.com"
15
+ SENDER_NAME = "DocuSort"
 
 
 
 
 
 
 
 
16
 
17
  def generate_otp(length=6):
18
  return ''.join(random.choices(string.digits, k=length))
19
 
20
  async def send_otp_email(email: EmailStr, otp: str, subject="DocuSort Verification"):
21
+ # Fallback safety: If API keys are missing, print to logs instead of crashing
22
+ if not MAILJET_API_KEY or not MAILJET_SECRET_KEY:
23
  print(f"\n{'='*40}")
24
+ print(f" ⚠️ [MOCK EMAIL - NO API KEY] To: {email}")
25
+ print(f" ⚠️ [MOCK EMAIL - NO API KEY] CODE: {otp}")
 
26
  print(f"{'='*40}\n")
27
  return
28
 
29
+ url = "https://api.mailjet.com/v3.1/send"
30
+ payload = {
31
+ "Messages": [
32
+ {
33
+ "From": {"Email": SENDER_EMAIL, "Name": SENDER_NAME},
34
+ "To": [{"Email": email}],
35
+ "Subject": subject,
36
+ "HTMLPart": f"<p>Your verification code is: <strong>{otp}</strong></p><p>This code expires in 10 minutes.</p>"
37
+ }
38
+ ]
39
+ }
40
 
41
+ async with httpx.AsyncClient() as client:
42
+ try:
43
+ response = await client.post(
44
+ url,
45
+ json=payload,
46
+ auth=(MAILJET_API_KEY, MAILJET_SECRET_KEY)
47
+ )
48
+ response.raise_for_status()
49
+ except Exception as e:
50
+ print(f"Mailjet API Error: {e}")
51
 
 
52
  async def send_alert_email(email: EmailStr, subject: str, body_content: str):
53
+ if not MAILJET_API_KEY or not MAILJET_SECRET_KEY:
 
 
 
 
54
  clean_body = body_content.replace("<br>", "\n").replace("<b style='font-size:24px;'>", "").replace("</b>", "")
55
+ print(f"\n{'='*40}")
56
+ print(f" ⚠️ [MOCK ALERT - NO API KEY] To: {email}")
57
+ print(f" ⚠️ [MOCK ALERT - NO API KEY] Body: \n{clean_body}")
58
  print(f"{'='*40}\n")
59
  return
60
 
61
+ url = "https://api.mailjet.com/v3.1/send"
62
  html_body = f"""
63
  <div style="font-family: Arial, sans-serif; padding: 20px; color: #333; max-width: 600px; margin: 0 auto; border: 1px solid #e2e8f0; border-radius: 8px;">
64
  <h2 style="color: #4f46e5; border-bottom: 2px solid #e0e7ff; padding-bottom: 10px;">DocuSort Notification</h2>
 
69
  </div>
70
  """
71
 
72
+ payload = {
73
+ "Messages": [
74
+ {
75
+ "From": {"Email": SENDER_EMAIL, "Name": SENDER_NAME},
76
+ "To": [{"Email": email}],
77
+ "Subject": subject,
78
+ "HTMLPart": html_body
79
+ }
80
+ ]
81
+ }
82
 
83
+ async with httpx.AsyncClient() as client:
84
+ try:
85
+ response = await client.post(
86
+ url,
87
+ json=payload,
88
+ auth=(MAILJET_API_KEY, MAILJET_SECRET_KEY)
89
+ )
90
+ response.raise_for_status()
91
+ except Exception as e:
92
+ print(f"Mailjet API Error: {e}")