"""SES utils.""" from email import encoders from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import boto3 class Email(object): """Email class using AWS SES.""" def __init__(self, ses_client=None): """Init. Args: ses_client: """ self.ses_client = ses_client or boto3.client( 'ses', region_name='us-east-1') def send(self, subject, from_address, recipients, body_text, attachment=None): """Sends multipart email. Args: subject (string): Email subject from_address (string|Address): From address recipients (list): List of recipients body_text (string): Email body attachment (dict): Attachment details Returns: dict: { 'MessageId': '', 'ResponseMetadata' : {} } """ msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = from_address msg['To'] = ', '.join(recipients) msg.attach(MIMEText(body_text, 'html')) if attachment: main_type, sub_type = attachment['mime_type'].split('/') part = MIMEBase(main_type, sub_type) part.set_payload(attachment['data']) encoders.encode_base64(part) part.add_header( 'Content-Disposition', 'attachment; filename="{}"'.format( attachment['file_name'] ) ) msg.attach(part) return self.ses_client.send_raw_email( Source=from_address, Destinations=recipients, RawMessage={'Data': msg.as_string()})