Extracting body from Outlook response email using python

Viewed 364

i'm writing a python script which reads emails from Outlook then extract the body The problem is that when it reads an email answer, the body contains the previous emails. Is there away to avoid that and just extract the body of the email.

This is a part of my code :

import requests
import json
import base64

utlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders.Item("UGAP-AMS-L2")
inbox = folder.Folders.Item("Inbox")
mails = inbox.Items
mails.Sort("[ReceivedTime]", False)

for mail in mails:
        if mail.UnRead == True  :
            print(" mail.Body")

This is what i get :

-Email body of the current email-

De : "tracker@gmail.fr" tracker@gmail.fr
Date : vendredi 21 mai 2021 à 08:44
À : Me Me@outlook.com
Objet : object

-body of previous email-

2 Answers

You would have to parse the body yourself to cut off anything you don't want.

What you want is not really possible - message body is a free-form text, the user is allowed to type anywhere. I personally do that all the time - I just type "see below" and insert my comments in the original email. There is no way to separate the two.

The Outlook object model (nor MAPI) doesn't provide anything for that.

There is no universal solution or specific property or method for getting the job done. You may try to find your own algorithm of extracting the latest content from the message body.

Related