"""AWS SES connector for sending emails."""
import csv
from datetime import datetime
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import io
from typing import List
import boto3
from lambdacommon.common_config import logger
def send_email_with_csv(
vendor_name: str,
emails_list: List[str],
to_addresses: List[str],
cc_addresses: List[str],
sender_email: str,
sender_display_name: str
) -> None:
"""Send email with CSV attachment containing deletion request emails.
Args:
vendor_name: Name of the vendor (e.g., 'AE/DELPHI', 'Wyng', etc.)
emails_list: List of email addresses to include in the CSV
to_addresses: List of recipient email addresses
cc_addresses: List of CC email addresses
sender_email: Sender's email address
sender_display_name: Display name for the sender
"""
if not emails_list:
logger.info(f'No emails to send for vendor: {vendor_name}')
return
# Create date string for subject and filename
today = datetime.now()
date_string = today.strftime('%m/%d/%Y')
file_date_string = today.strftime('%m.%d.%Y')
# Create subject and filename
subject = f'Sony Music Fan Delete Request - {vendor_name} - {date_string}'
filename = f'Sony Music Fan Delete Request - {vendor_name} - {file_date_string}.csv'
# Create CSV content
csv_content = create_csv_content(emails_list)
# Create email body
body = (
f'
Please delete the fans in the attached file from all '
f'business units/properties/territories in {vendor_name}.'
'
Please reply to confirm when this is completed.'
'
Thanks,
Sony Music CRM Support
'
)
# Send email
send_email_via_ses(
subject=subject,
body=body,
to_addresses=to_addresses,
cc_addresses=cc_addresses,
sender_email=sender_email,
sender_display_name=sender_display_name,
csv_filename=filename,
csv_content=csv_content
)
logger.info(f'Sent email to {vendor_name} with {len(emails_list)} email(s)')
def create_csv_content(emails_list: List[str]) -> bytes:
"""Create CSV content with Email_Address column.
Args:
emails_list: List of email addresses
Returns:
CSV content as bytes
"""
output = io.StringIO()
csv_writer = csv.writer(output, quoting=csv.QUOTE_ALL)
# Write header
csv_writer.writerow(['Email_Address'])
# Write email addresses
for email in emails_list:
csv_writer.writerow([email])
csv_string = output.getvalue()
output.close()
return csv_string.encode('utf-8')
def send_email_via_ses(
subject: str,
body: str,
to_addresses: List[str],
cc_addresses: List[str],
sender_email: str,
sender_display_name: str,
csv_filename: str,
csv_content: bytes
) -> None:
"""Send email via AWS SES with CSV attachment.
Args:
subject: Email subject
body: HTML body content
to_addresses: List of recipient email addresses
cc_addresses: List of CC email addresses
sender_email: Sender's email address
sender_display_name: Display name for the sender
csv_filename: Name of the CSV file
csv_content: CSV file content as bytes
"""
ses_client = boto3.client('ses', region_name='us-east-1')
# Create a multipart message
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = f'{sender_display_name} <{sender_email}>'
msg['To'] = ', '.join(to_addresses)
if cc_addresses:
msg['Cc'] = ', '.join(cc_addresses)
# Add HTML body
html_part = MIMEText(body, 'html')
msg.attach(html_part)
# Add CSV attachment
attachment = MIMEApplication(csv_content)
attachment.add_header('Content-Disposition', 'attachment', filename=csv_filename)
msg.attach(attachment)
# Combine To and CC addresses for sending
all_recipients = to_addresses + cc_addresses
# Send email
try:
response = ses_client.send_raw_email(
Source=sender_email,
Destinations=all_recipients,
RawMessage={'Data': msg.as_string()}
)
logger.info(f'Email sent successfully. MessageId: {response["MessageId"]}')
except Exception as e:
logger.error(f'Failed to send email: {str(e)}')
raise