File size: 1,613 Bytes
725fc68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b7c513
 
725fc68
 
 
 
 
 
 
 
 
 
 
2b7c513
 
725fc68
 
 
 
 
 
 
 
 
 
4c18e6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)}")