Spaces:
Sleeping
Sleeping
| from fastapi import HTTPException | |
| from fastapi.templating import Jinja2Templates | |
| from email.mime.text import MIMEText | |
| import smtplib | |
| import logging | |
| import os | |
| # Set up logging | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
| # Set up Jinja2 templates | |
| templates = Jinja2Templates(directory="templates") | |
| async def send_rcs_email(to_email: str, direct_link: str): | |
| """Send an email with a direct link to view the generated RCS cards.""" | |
| try: | |
| from_email = os.getenv("GMAIL_FROM_EMAIL") | |
| password = os.getenv("GMAIL_APP_PASSWORD") | |
| subject = "Your RCS Cards Have Been Generated" | |
| # Render the email template | |
| template = templates.get_template("email.html") | |
| body = template.render(direct_link=direct_link) | |
| msg = MIMEText(body, "html") | |
| msg['Subject'] = subject | |
| msg['From'] = from_email | |
| msg['To'] = to_email | |
| with smtplib.SMTP_SSL('smtp.gmail.com', 587) as server: | |
| server.starttls() | |
| server.login(from_email, password) | |
| server.sendmail(from_email, to_email, msg.as_string()) | |
| logging.info(f"Email sent successfully to {to_email}") | |
| except smtplib.SMTPAuthenticationError: | |
| logging.error("Failed to authenticate with SMTP server.") | |
| raise HTTPException(status_code=500, detail="Failed to authenticate with SMTP server. Check email credentials.") | |
| except Exception as e: | |
| logging.error(f"Failed to send email: {str(e)}") | |
| raise HTTPException(status_code=500, detail=f"Failed to send email: {str(e)}") |