Spaces:
Sleeping
Sleeping
Upload send_email.py
Browse files- send_email.py +41 -0
send_email.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)}")
|