I am trying to get the contents of email originally sent to a user which is forwarded to my current account. I am using gmail api to do so.
But I am unable to filter the extra text that comes with it. This is my code to access one email by one message id at a time.
import lxml.html
from bs4 import BeautifulSoup
myid = "18147a05aba83e45"
# service is the gmail service which is necessary to access gmail api i have not included the code to get service
txt = service.users().messages().get(userId='me', id=myid).execute()
try:
if 'INBOX' in txt['labelIds']:
payload = txt['payload']
parts = payload.get('parts')[0]
data = parts['body']['data']
data = data.replace("-","+").replace("_","/")
decoded_data = base64.b64decode(data)
soup = BeautifulSoup(decoded_data , "lxml")
body = soup.body()
body = str(body)
body = body[1:-1]
my_msg = lxml.html.fromstring(body).text_content()
print(my_msg)
except Exception as e:
print('exception', e)
This is the email sent to user.
Hshhsjshshhsbsbhs d dddd
1.5.5.5
2. D d. Sbsh
This is the output from my code
-----Forwarded message-----
From: NiKHiL
Date: Mon, Jul 11, 2822, 12:42 PM
Subject: 0eiginal sub
To: nikhilchauhanxd@gmail.com
Hshhsjshshhsbsbhs d dddd
1.5.5.5
2. D d. Sbsh
,
Date: Mon, Jul 11, 2022, 12:42 PM
Subject: Oeiginal sub
To: nikhilchauhanxd@gnail.com
Hshhsjshshhsbsbhs d dddd
1.5.5.5
2. D d. Sbsh
,
Hshhsjshshhsbsbhs d dddd
1.5.5.5
2. D d. Sbsh
As you can see in the output original message is repeated many times and comes with extra text.
Any help will be appreciated. Thanks in advance.