- 使用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)
- 使用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 =