使用AWS SES向熊猫数据框发送电子邮件时出现问题

时间:2019-09-20 18:00:48

标签: html python-3.x pandas amazon-web-services amazon-ses

我有一个Pandas数据框,我想使用AWS Simple Email Service通过电子邮件发送出去。

我这样制作数据框:

df = pd.DataFrame.from_records(listOfLists)
df_html = df.to_html()
send_email(df_html)

这是我尝试实际发送数据帧的方式:

import boto3
from botocore.exceptions import ClientError


def send_email(html_string):
    # Replace sender@example.com with your "From" address.
    # This address must be verified with Amazon SES.
    SENDER = "fake_email@fakeplace.com"

    # Replace recipient@example.com with a "To" address. If your account
    # is still in the sandbox, this address must be verified.
    RECIPIENT = "fake_email@fakeplace.com"

    # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
    AWS_REGION = "us-east-1"

    # The subject line for the email.
    SUBJECT = "Amazon SES Test (SDK for Python)"

    # The email body for recipients with non-HTML email clients.
    BODY_TEXT = ("Amazon SES Test (Python)\r\n"
                 "This email was sent with Amazon SES using the "
                 "AWS SDK for Python (Boto)."
                 )

    # The HTML body of the email.
    BODY_HTML = f"""<html>
    <head></head>
    <body>
    {html_string}
    </body>
    </html> """

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

    # Create a new SES resource and specify a region.
    client = boto3.client('ses', region_name=AWS_REGION)

    # Try to send the email.
    try:
        # Provide the contents of the email.
        response = client.send_email(
            Destination={
                'ToAddresses': [
                    RECIPIENT,
                ],
            },
            Message={
                'Subject' : {'Data' : SUBJECT},
                'Body' : {
                    'Html' : {'Data' : BODY_HTML}
                }
            },
            Source=SENDER,
        )
    # Display an error if something goes wrong.
    except ClientError as e:
        print(e.response['Error']['Message'])
    else:
        print("Email sent! Message ID:"),
        print(response['MessageId'])

您会发现它与您在此处看到的示例非常相似:https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-python.html

此代码只会生成一封包含空白内容的电子邮件。对于为什么会产生空电子邮件的任何见解,将不胜感激。

0 个答案:

没有答案