如何通过AWS SES向多个收件人发送电子邮件

时间:2019-08-30 08:43:23

标签: amazon-web-services python-3.7 amazon-ses

大家好,我尝试使用Python通过AWS SES向多个用户发送电子邮件,但是每当我尝试发送邮件时,我都会收到错误消息:非法地址

这是我的代码:

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

我已经在互联网上进行搜索,但没有答案有帮助。这是我尝试过https://www.jeffgeerling.com/blogs/jeff-geerling/sending-emails-multiple的另一种方式,但这也无济于事,请帮助我哪里做错了我在哪里修改它,请ping ping me对此有任何疑问...谢谢。

3 个答案:

答案 0 :(得分:2)

在我看来,您应该传递“收件人”而不是RECIPENT字符串。尝试这样的事情:

Destination={'ToAddresses':recipients}

似乎期望使用数组,而不是逗号分隔的字符串列表。

答案 1 :(得分:1)

boto3 SES send_email documentation中:

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

如果您阅读SES SendEmail API call documentation,它会告诉您Destination object是:

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

总结:不要加入地址来构造RECIPIENT。 RECIPIENT必须是一个字符串数组,每个字符串一个地址。

答案 2 :(得分:0)

RECIPIENT必须为字符串数组> ['email1','email2']

和>>

Destination={
            'ToAddresses': [
                RECIPIENT,
            ],
        },

Destination={
            'ToAddresses': RECIPIENT
        },