Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email to multiple recipients through AWS SES

Hello guys i'm trying to send email to multiple user through AWS SES using Python but whenever i'm trying to send a mail i got a error : Illegal address

This is my code:

def emailServiceForCustomerInformation(self, emailSubject, customerLicenseMessage, installation_name):
    # logger = ToolsLogger.getOrCreateLogger(current_user.keyspace)
    logger = ToolsLogger.getOrCreateRootLogger()

    logger.info("Email service For Customer is started")

      record = int(recordCount)
    # print("emailRcord-",record)

    # This address must be verified with Amazon SES.
    SENDER = "Snehil singh<[email protected]>"

    # is still in the sandbox, this address must be verified.

    recipients = ["[email protected]","[email protected]"]
    RECIPIENT = ", ".join(recipients)

    # If necessary, replace us-east-1 with the AWS Region currently using for Amazon SES.
    AWS_REGION = "us-east-1"

    # The subject line for the email.
    SUBJECT = emailSubject

    BODY_TEXT = (customerLicenseMessage + ' ''For InstallationName-'+ installation_name)

    # The character encoding for the email.
    CHARSET = "UTF-8"

    client = boto3.client('ses', region_name=AWS_REGION,
                          aws_access_key_id=config[os.environ['CONFIG_TYPE']].S3_ACCESS_KEY,
                          aws_secret_access_key=config[os.environ['CONFIG_TYPE']].S3_ACCESS_SECRET_KEY,
                          config=Config(signature_version='s3v4'))

    is_success = True
    # Try to send the email.
    try:
        # Provide the contents of the email.
        response = client.send_email(
            Destination={
                'ToAddresses': [
                    RECIPIENT,
                ],
            },
            Message={
                'Body': {
                    'Text': {
                        'Charset': CHARSET,
                        'Data': BODY_TEXT,
                    },
                },
                'Subject': {
                    'Charset': CHARSET,
                    'Data': SUBJECT,
                },
            },
            Source=SENDER,
            # If you are not using a configuration set, comment or delete the
            # following line
            #         ConfigurationSetName=CONFIGURATION_SET,
        )
    # Display an error if something goes wrong.
    except ClientError as e:
        logger.exception(e)
        print(e.response['Error']['Message'])
        is_success = False
    else:
        # print("Email sent! Message ID:"),
        # print(response['MessageId'])
        logger.info("Email service is Completed and send to the mail")

    return is_success

i have searched on internet but non of the answer helped This is another way i have tried https://www.jeffgeerling.com/blogs/jeff-geerling/sending-emails-multiple but this also not helpful please help me where i'm doing wrong where do i modify it please ping me if you have any questions related this...thanks in advance.

like image 970
snehil singh Avatar asked Mar 04 '23 12:03

snehil singh


2 Answers

Looks to me like you should be passing in the 'recipient', not the RECIPENT string. Try something like this:

Destination={'ToAddresses':recipients}

It appears to be expecting an array, not a comma seperated list of strings.

like image 63
E.J. Brennan Avatar answered Mar 06 '23 00:03

E.J. Brennan


In boto3 SES send_email documentation:

response = client.send_email(
    Source='string',
    Destination={
        'ToAddresses': [
            'string',
        ],
        'CcAddresses': [
            'string',
        ],
        'BccAddresses': [
            'string',
        ]
    },

And if you read the SES SendEmail API call documentation, it tells you that the Destination object is:

BccAddresses.member.N

    The BCC: field(s) of the message.

    Type: Array of strings

    Required: No
CcAddresses.member.N

    The CC: field(s) of the message.

    Type: Array of strings

    Required: No
ToAddresses.member.N

    The To: field(s) of the message.

    Type: Array of strings

    Required: No

In summary: don't join the address to construct RECIPIENT. RECIPIENT needs to be an array (a list, in Python) of strings, where each strings is one email address.

like image 45
Diego Fernández Durán Avatar answered Mar 06 '23 00:03

Diego Fernández Durán