AWSLambda如何读取带附件的AWSWorkMail邮件?
创始人
2024-09-26 07:30:30
0
  1. 使用AWS SDK for Python (Boto 3)来读取AWS WorkMail邮件和附件:
import boto3
from botocore.exceptions import ClientError

def read_workmail_mail_with_attachment():
    try:
        # Create a WorkMail client
        workmail_client = boto3.client('workmail')

        # Get the messageId of the email to read
        message_id = 'my-message-id'

        # Get the content of the email message
        response = workmail_client.get_raw_message_content(
            messageId=message_id
        )

        # Get the email content and attachments
        email_content = response['messageContent'].read()

        # Save the email content to a file
        with open('email_content.eml', 'wb') as f:
            f.write(email_content)

        # Parse the email content using the Python email library
        import email
        message = email.message_from_bytes(email_content)

        # Get the attachments from the email message
        attachments = []

        for part in message.walk():
            if part.get_content_maintype() == 'multipart':
                continue

            filename = part.get_filename()

            if not filename:
                continue

            attachment = {
                'filename': filename,
                'content': part.get_payload(decode=True),
                'type': part.get_content_type()
            }

            attachments.append(attachment)

        # Print the attachments
        print(attachments)

    except ClientError as e:
        print(e)
  1. 使用AWS Lambda和AWS SDK for Python (Boto 3)将以上代码部署到AWS Lambda:
import json
import boto3
from botocore.exceptions import ClientError

def lambda_handler(event, context):
    try:
        # Create a WorkMail client
        workmail_client = boto3.client('workmail')

        # Get the messageId of the email to read
        message_id = 'my-message-id'

        # Get the content of the email message
        response = workmail_client.get_raw_message_content(
            messageId=message_id
        )

        # Get the email content and attachments
        email_content = response['messageContent'].read()

        # Parse the email content using the Python email library
        import email
        message =

相关内容

热门资讯

一分钟了解!雷霆辅助,杭州边锋... 一分钟了解!雷霆辅助,杭州边锋网络游戏辅助,辅助教程(新版有挂)-哔哩哔哩;1.杭州边锋网络游戏辅助...
一分钟了解!新二号辅助下载,心... 一分钟了解!新二号辅助下载,心悦填大坑辅助,透明挂教程(有挂方法)-哔哩哔哩;1、这是跨平台的心悦填...
透视最新!决战卡五星游戏辅助器... 透视最新!决战卡五星游戏辅助器,科乐天天踢起手好牌,黑科技教程(有挂教程)-哔哩哔哩;最新版2026...
分辨真假!一乐浙江辅助软件,茶... 【福星临门,好运相随】;分辨真假!一乐浙江辅助软件,茶馆游戏辅助,分享教程(有挂讲解)-哔哩哔哩;暗...
透视美元局!蜜瓜大厅辅助免费下... 透视美元局!蜜瓜大厅辅助免费下载,海盗来了大白辅助,实用技巧(有挂透视)-哔哩哔哩;致您一封信;亲爱...
最新通报!潮友会透视辅助教程,... 最新通报!潮友会透视辅助教程,天天爱柳州辅助器,教你教程(有挂分享)-哔哩哔哩,潮友会透视辅助教程是...
透视黑科技!微信小程序蜀山辅助... 《透视黑科技!微信小程序蜀山辅助器免费下载,填大坑辅助器视频,大神讲解(讲解有挂)-哔哩哔哩》 微信...
科技新动态!丹东约战麻将辅助器... 科技新动态!丹东约战麻将辅助器,欢乐情怀辅助挂,攻略方法(讲解有挂)-哔哩哔哩;丹东约战麻将辅助器A...
透视真的!潮汕掌上娱透视软件,... 透视真的!潮汕掌上娱透视软件,新上游辅助器,揭秘教程(有挂技巧)-哔哩哔哩;1、不需要AI权限,帮助...
实测分享!hhpoker智能辅... 实测分享!hhpoker智能辅助插件,wepoker辅助透视,必备教程(一贯真的是有挂)-哔哩哔哩;...