sumanth shetty
sumanth shetty

Reputation: 2181

How to send a email to multiple email address using SES in Lambda function python

I am trying to have an event-driven mail notification sent to all the people how have subscribed to the mail notification.

here is my code.

import json
import boto3
import uuid


def lambda_handler(event, context):
    item = json.loads(event['body'])
    amount = item['bid_amount']
    broker = item['posted_by_company']
    email = ["sumanth@xxxxxxx", "[email protected]"]
    email = ','.join(email)
    print (type(email))
    ses = boto3.client("ses")
    try:
        
        response = ses.send_email(
            Source = "xxxxxx@xxxxxx",
            Destination={
                'ToAddresses': [
                    email
                   
                ],
                'CcAddresses': [
                
                ]
            },
            Message={
                'Subject': {
                    'Data': "Your Bid has been Accepted" 
                },
                'Body': {
                    'Text': {
                        'Data': "Your Bid of amount $"+ amount +" has been accepted by " + broker + "\n"+
                        "Here are the Load details:\n" + 
                        "Load ID: \n" +
                        "Posted by: \n" 
                        
                    }
                }
            }
        )
        
        return {
            'statusCode': 200,
            'headers': {"Access-Control-Allow-Origin": "*"},
            'body': json.dumps('e-mail sent ')
        }
    except Exception as e:
        print(e)
        return {
            
            'statusCode': 500,
            'headers': {"Access-Control-Allow-Origin": "*"},
            'body': json.dumps('Error occured while sending an Bid e-mail')
        }

It seems to address takes an only string object. How do I pass all the e-mail received by an event in toAddress.?

Upvotes: 0

Views: 1354

Answers (1)

Marcin
Marcin

Reputation: 238747

If your email actually has the following form as you wrote:

email = ["sumanth@xxxxxxx", "[email protected]"]

then you can just pass it directly:

            Destination={
                'ToAddresses': email,

No need for email = ','.join(email).

Upvotes: 1

Related Questions