Spaces:
Sleeping
Sleeping
Update send_email.py
Browse files- 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 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
msg =
|
26 |
-
msg['
|
27 |
-
msg['
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
server.
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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)}")
|