How to fetch the latest email from thread in AWS Workmail

45 views Asked by At

Im trying to get only the latest response from incoming emails in AWS Workmail but my code currently fetches the whole email thread. I have an inbound rule that sends the incoming emails to a Lambda

client = boto3.client('workmailmessageflow')
response = client.get_raw_message_content(messageId=message_id)
    
message_bytes = response['messageContent'].read()
message_content = message_from_bytes(message_bytes)
    
message = ""
    
# Iterate through the MIME message parts
for part in message_content.walk():
    if part.get_content_type() == "text/plain":
        try:
            message = part.get_payload(decode=True).decode('utf-8')
        except UnicodeDecodeError:
            continue
        break  # Only get the first "text/plain" content; ignore the rest to avoid fetching the whole thread

How do I get only the latest response? I imagined this to be a simple task as the email client already knows what response is the newest but I guess not :( I could try regex but its likely to fail as emails are formatted differently depending on email provider

0

There are 0 answers