How to send email to multiple recipients through AWS SES

与世无争的帅哥 提交于 2020-02-28 08:47:28

问题


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<snehil@codedata.io>"

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

    recipients = ["cosmoandysysmo@gmail.com","snehil@codedata.io"]
    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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/57723411/how-to-send-email-to-multiple-recipients-through-aws-ses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!