Vishwanath77 commited on
Commit
fcbfb48
·
verified ·
1 Parent(s): c857d7d

Update src/apps/email_utils.py

Browse files
Files changed (1) hide show
  1. src/apps/email_utils.py +66 -65
src/apps/email_utils.py CHANGED
@@ -1,65 +1,66 @@
1
- from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType
2
- from pydantic import EmailStr
3
- import os
4
- from dotenv import load_dotenv
5
-
6
- load_dotenv()
7
-
8
- BASE_URL = os.getenv("BASE_URL", "http://localhost:8000")
9
-
10
- # Support both SMTP_* and MAIL_* environment variable naming conventions
11
- MAIL_USERNAME = os.getenv("MAIL_USERNAME") or os.getenv("SMTP_USERNAME", "")
12
- MAIL_PASSWORD = os.getenv("MAIL_PASSWORD") or os.getenv("SMTP_PASSWORD", "")
13
- MAIL_FROM = os.getenv("MAIL_FROM") or os.getenv("FROM_EMAIL", "")
14
- MAIL_PORT = int(os.getenv("MAIL_PORT") or os.getenv("SMTP_PORT", "587"))
15
- MAIL_SERVER = os.getenv("MAIL_SERVER") or os.getenv("SMTP_SERVER", "smtp.gmail.com")
16
-
17
- conf = ConnectionConfig(
18
- MAIL_USERNAME=MAIL_USERNAME,
19
- MAIL_PASSWORD=MAIL_PASSWORD,
20
- MAIL_FROM=MAIL_FROM,
21
- MAIL_PORT=MAIL_PORT,
22
- MAIL_SERVER=MAIL_SERVER,
23
- MAIL_STARTTLS=True,
24
- MAIL_SSL_TLS=False,
25
- USE_CREDENTIALS=True,
26
- VALIDATE_CERTS=True
27
- )
28
-
29
- async def send_reset_email(email: EmailStr, token: str):
30
- # Validate email configuration before attempting to send
31
- if not MAIL_USERNAME or not MAIL_PASSWORD or not MAIL_FROM:
32
- missing_vars = []
33
- if not MAIL_USERNAME:
34
- missing_vars.append("MAIL_USERNAME or SMTP_USERNAME")
35
- if not MAIL_PASSWORD:
36
- missing_vars.append("MAIL_PASSWORD or SMTP_PASSWORD")
37
- if not MAIL_FROM:
38
- missing_vars.append("MAIL_FROM or FROM_EMAIL")
39
- raise ValueError(f"Email configuration missing: {', '.join(missing_vars)}. Please set these environment variables.")
40
-
41
- reset_link = f"{BASE_URL}/reset-password?token={token}"
42
- html_body = f"""
43
- <html>
44
- <body style="font-family: Arial, sans-serif; padding: 20px; background-color: #f4f4f4;">
45
- <div style="max-width: 600px; margin: 0 auto; background-color: white; padding: 30px; border-radius: 10px;">
46
- <h2 style="color: #9b87f5;">Password Reset Request</h2>
47
- <p>Hello,</p>
48
- <p>We received a request to reset your password for your Law Bot account.</p>
49
- <p>Click the button below to reset your password:</p>
50
- <a href="{reset_link}" style="display: inline-block; padding: 12px 24px; background-color: #9b87f5; color: white; text-decoration: none; border-radius: 6px; margin: 20px 0;">Reset Password</a>
51
- <p>Or copy and paste this link into your browser:</p>
52
- <p style="color: #666; word-break: break-all;">{reset_link}</p>
53
- <p style="color: #999; font-size: 12px; margin-top: 30px;">If you didn't request this, you can safely ignore this email.</p>
54
- </div>
55
- </body>
56
- </html>
57
- """
58
- message = MessageSchema(
59
- subject="Password Reset Request - Law Bot",
60
- recipients=[email],
61
- body=html_body,
62
- subtype=MessageType.html
63
- )
64
- fm = FastMail(conf)
65
- await fm.send_message(message)
 
 
1
+ from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType
2
+ from pydantic import EmailStr
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+
9
+ BASE_URL = os.getenv("APP_URL", "http://localhost:8000")
10
+
11
+ # Support both SMTP_* and MAIL_* environment variable naming conventions
12
+ MAIL_USERNAME = os.getenv("MAIL_USERNAME") or os.getenv("SMTP_USERNAME", "")
13
+ MAIL_PASSWORD = os.getenv("MAIL_PASSWORD") or os.getenv("SMTP_PASSWORD", "")
14
+ MAIL_FROM = os.getenv("MAIL_FROM") or os.getenv("FROM_EMAIL", "")
15
+ MAIL_PORT = int(os.getenv("MAIL_PORT") or os.getenv("SMTP_PORT", "587"))
16
+ MAIL_SERVER = os.getenv("MAIL_SERVER") or os.getenv("SMTP_SERVER", "smtp.gmail.com")
17
+
18
+ conf = ConnectionConfig(
19
+ MAIL_USERNAME=MAIL_USERNAME,
20
+ MAIL_PASSWORD=MAIL_PASSWORD,
21
+ MAIL_FROM=MAIL_FROM,
22
+ MAIL_PORT=MAIL_PORT,
23
+ MAIL_SERVER=MAIL_SERVER,
24
+ MAIL_STARTTLS=True,
25
+ MAIL_SSL_TLS=False,
26
+ USE_CREDENTIALS=True,
27
+ VALIDATE_CERTS=True
28
+ )
29
+
30
+ async def send_reset_email(email: EmailStr, token: str):
31
+ # Validate email configuration before attempting to send
32
+ if not MAIL_USERNAME or not MAIL_PASSWORD or not MAIL_FROM:
33
+ missing_vars = []
34
+ if not MAIL_USERNAME:
35
+ missing_vars.append("MAIL_USERNAME or SMTP_USERNAME")
36
+ if not MAIL_PASSWORD:
37
+ missing_vars.append("MAIL_PASSWORD or SMTP_PASSWORD")
38
+ if not MAIL_FROM:
39
+ missing_vars.append("MAIL_FROM or FROM_EMAIL")
40
+ raise ValueError(f"Email configuration missing: {', '.join(missing_vars)}. Please set these environment variables.")
41
+
42
+ reset_link = f"{BASE_URL}/reset-password?token={token}"
43
+ html_body = f"""
44
+ <html>
45
+ <body style="font-family: Arial, sans-serif; padding: 20px; background-color: #f4f4f4;">
46
+ <div style="max-width: 600px; margin: 0 auto; background-color: white; padding: 30px; border-radius: 10px;">
47
+ <h2 style="color: #9b87f5;">Password Reset Request</h2>
48
+ <p>Hello,</p>
49
+ <p>We received a request to reset your password for your Law Bot account.</p>
50
+ <p>Click the button below to reset your password:</p>
51
+ <a href="{reset_link}" style="display: inline-block; padding: 12px 24px; background-color: #9b87f5; color: white; text-decoration: none; border-radius: 6px; margin: 20px 0;">Reset Password</a>
52
+ <p>Or copy and paste this link into your browser:</p>
53
+ <p style="color: #666; word-break: break-all;">{reset_link}</p>
54
+ <p style="color: #999; font-size: 12px; margin-top: 30px;">If you didn't request this, you can safely ignore this email.</p>
55
+ </div>
56
+ </body>
57
+ </html>
58
+ """
59
+ message = MessageSchema(
60
+ subject="Password Reset Request - Law Bot",
61
+ recipients=[email],
62
+ body=html_body,
63
+ subtype=MessageType.html
64
+ )
65
+ fm = FastMail(conf)
66
+ await fm.send_message(message)