RCS / send_email.py
AiDeveloper1's picture
Upload send_email.py
4c18e6b verified
raw
history blame
1.69 kB
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 = "harshitnayak45@gmail.com" # Replace with your Gmail address
password = "tjbj oipm kblw phaa" # Replace with the 16-character 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', 465) as server:
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)}")