【问题标题】:AWS Lambda SES send email with attachment PYTHONAWS Lambda SES 发送带有附件 PYTHON 的电子邮件
【发布时间】:2019-12-07 18:09:12
【问题描述】:

我的 Lambda 函数在 lambda 环境中的 /tmp 位置创建一个文本文件。

我无法使用 Lambda SES Python 将文件作为附件发送

我正在遵循 AWS 文档下方的代码,以使用 SES Python3.6 发送带有附件的 RAW 电子邮件

https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-raw-using-sdk.html

def send_mail_with_attach_ses(sender, recipient, aws_region, subject, file_name):
    # The email body for recipients with non-HTML email clients.
    BODY_TEXT = "Hello,\r\nPlease find the attached file."

    # The HTML body of the email.
    BODY_HTML = """\
    <html>
    <head></head>
    <body>
    <h1>Hello!</h1>
    <p>Please find the attached file.</p>
    </body>
    </html>
    """

    CHARSET = "utf-8"
    client = boto3.client('ses',region_name=aws_region)

    msg = MIMEMultipart('mixed')
    # Add subject, from and to lines.
    msg['Subject'] = subject 
    msg['From'] = sender 
    msg['To'] = recipient    

    msg_body = MIMEMultipart('alternative')
    textpart = MIMEText(BODY_TEXT.encode(CHARSET), 'plain', CHARSET)
    htmlpart = MIMEText(BODY_HTML.encode(CHARSET), 'html', CHARSET)

    # Add the text and HTML parts to the child container.
    msg_body.attach(textpart)
    msg_body.attach(htmlpart)

    # Define the attachment part and encode it using MIMEApplication.
    att = MIMEApplication(open(file_name, 'rb').read())

    att.add_header('Content-Disposition','attachment',filename=os.path.basename(file_name))

    if os.path.exists(file_name):
        print("File exists")
    else:
        print("File does not exists")

    # Attach the multipart/alternative child container to the multipart/mixed
    # parent container.
    msg.attach(msg_body)

    # Add the attachment to the parent container.
    msg.attach(att)

    try:
        #Provide the contents of the email.
        response = client.send_raw_email(
            Source=msg['From'],
            Destinations=[
                msg['To']
            ],
            RawMessage={
                'Data':msg.as_string(),
            },
            ConfigurationSetName="ConfigSet"
        )
    # 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'])


    # Add a header to tell the email client to treat this part as an attachment,
    # and to give the attachment a name.
    att.add_header('Content-Disposition','attachment',filename=os.path.basename(ATTACHMENT))

    # Attach the multipart/alternative child container to the multipart/mixed
    # parent container.
    msg.attach(msg_body)

    # Add the attachment to the parent container.
    msg.attach(att)

触发 lambda 后,函数到达 response = client.send_raw_email 并且在 lambda 超时之前永远不会返回。我的 lambda 超时配置为 3 分钟。

{
  "errorMessage": "2019-07-30T10:14:45.180Z fc3ca995-58f9-4f89-8a35-114d941ee063 Task timed out after 180.06 seconds"
}

谁能帮助使用 Lambda SES Python 发送带有附件的电子邮件?

【问题讨论】:

  • 文件在哪里,大小是多少?
  • 你的 lambda 能否到达 ses 端点。您是否在 VPC 中运行?
  • @abdullahkhawer 文件很小,大约 10KB
  • @titogeo - 是的,我的 lambda 在 vpc 中,我的 vpc 有带互联网网关的路由表,并连接到 vpc 中的所有子网。如何检查 lambda 和 ses 端点之间的连通性?
  • @user3276214 文件在哪里?

标签: python-3.x amazon-web-services aws-lambda email-attachments amazon-ses


【解决方案1】:

你只需要在函数的基本设置下扩展超时值。最长可达 15 分钟。

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 2022-11-25
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多