【问题标题】:How to send email to multiple recipients through AWS SES如何通过 AWS SES 向多个收件人发送电子邮件
【发布时间】:2019-08-30 08:43:23
【问题描述】:

大家好,我正在尝试使用 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 但这也没有帮助请帮助我哪里做错了我在哪里修改它如果你有任何问题请联系我与此相关的问题...在此先感谢。

【问题讨论】:

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


    【解决方案1】:

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

    Destination={'ToAddresses':recipients}
    

    它似乎需要一个数组,而不是逗号分隔的字符串列表。

    【讨论】:

    • 谢谢解决方案
    • 我可以使用这种方法向多个收件人发送电子邮件,但是当我将电子邮件发送到分发列表时,它就不起作用了。它也没有给出任何错误。
    【解决方案2】:

    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 需要是一个字符串数组(Python 中的列表),其中每个字符串是一个电子邮件地址。

    【讨论】:

    • F.Duran thnx 的答案
    【解决方案3】:

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

    和>>

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

    Destination={
                'ToAddresses': RECIPIENT
            },
    

    【讨论】:

      猜你喜欢
      • 2019-11-05
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2019-03-01
      • 2016-11-17
      • 2012-05-18
      相关资源
      最近更新 更多