"""SES connector.""" import boto3 from notifications.config import AWS_REGION, SES_CHARSET, SES_SENDER client = boto3.client('ses', region_name=AWS_REGION) def send_email( subject: str, text_body: str, recipient: str, cc_recipients: list[str] | None = None, source: str = SES_SENDER, ) -> None: """Send a text-based email via SES. Args: subject (str): subject of the email to send text_body (str): text body of the email to send recipient (str): email address for the recipient of the email cc_recipients ([str]): Optional list of cc recipients source (str): the source of the email """ destination = {'ToAddresses': [recipient]} if cc_recipients: destination['CcAddresses'] = cc_recipients client.send_email( Destination=destination, Message={ 'Body': {'Text': {'Charset': SES_CHARSET, 'Data': text_body}}, 'Subject': {'Charset': SES_CHARSET, 'Data': subject}, }, Source=source, ) def send_html_email( subject: str, body: str, recipient: str, source: str = SES_SENDER, bcc: list[str] | None = None ) -> None: """Send a html formatted email via SES. Args: subject (str): subject of the email to send body (str): html content to send recipient (str): email address for the recipient of the email source (str): email address for the sender of the email bcc (list[str]): a list of email addresses to BCC """ destination = {'ToAddresses': [recipient]} if bcc: destination['BccAddresses'] = bcc client.send_email( Destination=destination, Message={ 'Body': {'Html': {'Charset': SES_CHARSET, 'Data': body}}, 'Subject': {'Charset': SES_CHARSET, 'Data': subject}, }, Source=source, ) def send_html_email_bulk( subject: str, body: str, recipients: list[str], cc: list[str] | None = None, bcc: list[str] | None = None, ) -> None: """Send a html formatted email via SES for multiple recipients. Args: subject (str): subject of the email to send body (str): html content to send recipients (List[str]): email address for the recipient of the email cc (List[str]): email address for the CC of the email bcc (List[str]): email address for the BCC of the email """ destination = {'ToAddresses': recipients} if cc: destination['CcAddresses'] = cc if bcc: destination['BccAddresses'] = bcc client.send_email( Destination=destination, Message={ 'Body': {'Html': {'Charset': SES_CHARSET, 'Data': body}}, 'Subject': {'Charset': SES_CHARSET, 'Data': subject}, }, Source=SES_SENDER, )