I use this function that AWS give as a tutorial: How can I read the files from Workmail that I stored in S3 bucket
raw_msg = workmail_message_flow.get_raw_message_content(messageId=message_id)
parsed_msg: Message = email.message_from_bytes(raw_msg['messageContent'].read())
# Updating subject. For more examples, see https://github.com/aws-samples/amazon-workmail-lambda-templates.
parsed_msg.replace_header('Subject', f"[Hello World!] {subject}")
# Try to get the email bucket.
updated_email_bucket_name = os.getenv('UPDATED_EMAIL_S3_BUCKET')
if not updated_email_bucket_name:
print('UPDATED_EMAIL_S3_BUCKET not set in environment. '
'Please follow https://docs.aws.amazon.com/lambda/latest/dg/env_variables.html to set it.')
return
key = str(uuid.uuid4())
# Put the message in S3, so WorkMail can access it.
s3.put_object(Body=parsed_msg.as_bytes(), Bucket=updated_email_bucket_name, Key=key)
# Update the email in WorkMail.
s3_reference = {
'bucket': updated_email_bucket_name,
'key': key
}
content = {
's3Reference': s3_reference
}
Every email is saved in my bucket but I can't open it and have no type so I can't convert it in a readable text.
Thank you for your time!
Being Python-ignorant, I offer a hint based on my reverse engineering of the same example to figure out how to do in C# what you want to do in Python.
email.message_from_bytes()
, from the Python Standard Library https://docs.python.org/3/library/email.parser.html returns a "message object structure" (email.Message?). But it (and the whole example) are of no use to you, because the raw message is available only in the context of an AWS Lambda handler implementing an incoming mail rule.I suggest that you start with an example of how to get an object from S3. An object from your WorkMail bucket will be what other libraries call a MimeMessage. Most of it (not attachments) will be text, but you may want to use email.parser on it to make it look more like what shows in your email client.