"""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, cc_list=[]): """Send multipart email. Args: subject (string) : Email subject from_address (str) : From address recipients (list) : List of To recipients body_text (string) : Email body attachment (dict) : Attachment details cc_list (list) : List of Cc recipients Returns: dict: { 'MessageId': '', 'ResponseMetadata' : {} } """ msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = from_address msg['To'] = ', '.join(recipients) if cc_list: msg['Cc'] = ', '.join(cc_list) 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 + cc_list, RawMessage={'Data': msg.as_string()})