AiDeveloper1 commited on
Commit
725fc68
·
verified ·
1 Parent(s): f7e37f9

Update send_email.py

Browse files
Files changed (1) hide show
  1. send_email.py +39 -40
send_email.py CHANGED
@@ -1,41 +1,40 @@
1
- from fastapi import HTTPException
2
- from fastapi.templating import Jinja2Templates
3
- from email.mime.text import MIMEText
4
- import smtplib
5
- import logging
6
- import os
7
-
8
- # Set up logging
9
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
-
11
- # Set up Jinja2 templates
12
- templates = Jinja2Templates(directory="templates")
13
-
14
- async def send_rcs_email(to_email: str, direct_link: str):
15
- """Send an email with a direct link to view the generated RCS cards."""
16
- try:
17
- from_email = "[email protected]" # Replace with your Gmail address
18
- password = "tjbj oipm kblw phaa" # Replace with the 16-character App Password
19
- subject = "Your RCS Cards Have Been Generated"
20
-
21
- # Render the email template
22
- template = templates.get_template("email.html")
23
- body = template.render(direct_link=direct_link)
24
-
25
- msg = MIMEText(body, "html")
26
- msg['Subject'] = subject
27
- msg['From'] = from_email
28
- msg['To'] = to_email
29
-
30
- with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
31
- server.login(from_email, password)
32
- server.sendmail(from_email, to_email, msg.as_string())
33
-
34
- logging.info(f"Email sent successfully to {to_email}")
35
-
36
- except smtplib.SMTPAuthenticationError:
37
- logging.error("Failed to authenticate with SMTP server.")
38
- raise HTTPException(status_code=500, detail="Failed to authenticate with SMTP server. Check email credentials.")
39
- except Exception as e:
40
- logging.error(f"Failed to send email: {str(e)}")
41
  raise HTTPException(status_code=500, detail=f"Failed to send email: {str(e)}")
 
1
+ from fastapi import HTTPException
2
+ from fastapi.templating import Jinja2Templates
3
+ from email.mime.text import MIMEText
4
+ import smtplib
5
+ import logging
6
+ import os
7
+
8
+ # Set up logging
9
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
+
11
+ # Set up Jinja2 templates
12
+ templates = Jinja2Templates(directory="templates")
13
+
14
+ async def send_rcs_email(to_email: str, direct_link: str):
15
+ """Send an email with a direct link to view the generated RCS cards."""
16
+ try:
17
+
18
+ subject = "Your RCS Cards Have Been Generated"
19
+
20
+ # Render the email template
21
+ template = templates.get_template("email.html")
22
+ body = template.render(direct_link=direct_link)
23
+
24
+ msg = MIMEText(body, "html")
25
+ msg['Subject'] = subject
26
+ msg['From'] = from_email
27
+ msg['To'] = to_email
28
+
29
+ with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
30
+ server.login(from_email, password)
31
+ server.sendmail(from_email, to_email, msg.as_string())
32
+
33
+ logging.info(f"Email sent successfully to {to_email}")
34
+
35
+ except smtplib.SMTPAuthenticationError:
36
+ logging.error("Failed to authenticate with SMTP server.")
37
+ raise HTTPException(status_code=500, detail="Failed to authenticate with SMTP server. Check email credentials.")
38
+ except Exception as e:
39
+ logging.error(f"Failed to send email: {str(e)}")
 
40
  raise HTTPException(status_code=500, detail=f"Failed to send email: {str(e)}")