Search code examples
pythonaws-lambdaamazon-ses

Not able to create boto ses client on AWS Lambda for sending mail


my python code is not working on Lambda which actully works well when I run it from my local python environment. Whenever I try to create SES object on Lambda function, I get this error:

Response:
{
  "errorMessage": "Unable to import module 'lambda_function'"
}

Here's my code:

def lambda_handler(event, context):

    connection = boto.ses.connect_to_region('us-east-1')


    return connection.send_email(
            from_addr,
            subject,
            None,
            to,
            format= format,
            text_body=text,
            html_body=html
        )   

Is it something related to boto.ses not being supported over lambda and I have to use boto3 instead??

This lambda function contains multiple part and at the end I have to create SES object to send mail to my client, but when I try to do that I'm getting this error


Solution

  • These days, it is highly recommended to use boto3.

    The syntax is:

    import boto3
    
    connection = boto3.client('ses', region_name='us-east-1')
    
    response = client.send_email(
        Source='string',
        Destination={
            'ToAddresses': [
                'string',
            ],
            'CcAddresses': [
                'string',
            ],
            'BccAddresses': [
                'string',
            ]
        },
        Message={
            'Subject': {
                'Data': 'string',
                'Charset': 'string'
            },
            'Body': {
                'Text': {
                    'Data': 'string',
                    'Charset': 'string'
                },
                'Html': {
                    'Data': 'string',
                    'Charset': 'string'
                }
            }
        },
        ReplyToAddresses=[
            'string',
        ],
        ReturnPath='string',
        SourceArn='string',
        ReturnPathArn='string',
        Tags=[
            {
                'Name': 'string',
                'Value': 'string'
            },
        ],
        ConfigurationSetName='string'
    )
    

    See: SES — Boto 3 Docs documentation