"""
Service for sending emails.
"""
import logging
import smtplib
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from jinja2 import Template
from app.core.config import settings
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Email template for analysis results
ANALYSIS_EMAIL_TEMPLATE = """
Analysis for messages from {{ start_date.strftime('%Y-%m-%d') }} to {{ end_date.strftime('%Y-%m-%d') }}
{{ analysis_text | safe }}
"""
async def send_email(
recipients: list[str],
subject: str,
html_content: str,
from_email: str | None = None,
from_name: str | None = None,
) -> bool:
"""
Send an email.
Args:
recipients: List of email addresses to send to
subject: Email subject
html_content: HTML content of the email
from_email: Sender email address (defaults to settings.EMAILS_FROM_EMAIL)
from_name: Sender name (defaults to settings.EMAILS_FROM_NAME)
Returns:
True if the email was sent successfully, False otherwise
"""
try:
# Use default sender details if not provided
sender_email = from_email or settings.EMAILS_FROM_EMAIL
sender_name = from_name or settings.EMAILS_FROM_NAME
# Create message
message = MIMEMultipart()
message["From"] = f"{sender_name} <{sender_email}>"
message["To"] = ", ".join(recipients)
message["Subject"] = subject
# Attach HTML content
message.attach(MIMEText(html_content, "html"))
# Connect to SMTP server and send email
with smtplib.SMTP(settings.SMTP_HOST, settings.SMTP_PORT) as server:
if settings.SMTP_TLS:
server.starttls()
if settings.SMTP_USER and settings.SMTP_PASSWORD:
server.login(settings.SMTP_USER, settings.SMTP_PASSWORD)
server.send_message(message)
logger.info(f"Email sent successfully to {len(recipients)} recipients")
return True
except Exception as e:
logger.exception(f"Failed to send email: {str(e)}")
return False
async def send_analysis_email(
subject: str,
analysis_text: str,
start_date: datetime,
end_date: datetime,
) -> bool:
"""
Send an email with message analysis.
Args:
subject: Email subject
analysis_text: Text of the analysis
start_date: Start date of the analysis period
end_date: End date of the analysis period
Returns:
True if the email was sent successfully, False otherwise
"""
try:
# Get recipients from settings
recipients = settings.EMAIL_RECIPIENTS
if not recipients:
logger.error("No email recipients configured")
return False
# Format HTML content using template
template = Template(ANALYSIS_EMAIL_TEMPLATE)
html_content = template.render(
analysis_text=analysis_text,
start_date=start_date,
end_date=end_date,
current_year=datetime.now().year,
)
# Send email
return await send_email(
recipients=recipients,
subject=subject,
html_content=html_content,
)
except Exception as e:
logger.exception(f"Error sending analysis email: {str(e)}")
return False