Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read emails python 3.
#6
(Jan-27-2019, 10:21 AM)negru555 Wrote: How can I just the content in <div dir="auto"></div> Bassicaly the message only.Also if the possible the subject but not neccesary.
Write your own parser,example BS for html and regex for rest.
from bs4 import BeautifulSoup
import re

email = '''\
MIME-Version: 1.0
Date: Sun, 27 Jan 2019 12:18:51 +0200
Message-ID: <CAK_qYVGw+UC7Ar9FhU+YRKQC8hXqs=tJzXW6_CvXsaJjzOJwSA@mail.gmail.com>
Subject: Muie
From: Wind Bullet <[email protected]>
To: [email protected]
Content-Type: multipart/alternative; boundary="0000000000000765de05806de3c2"

--0000000000000765de05806de3c2
Content-Type: text/plain; charset="UTF-8"

Muiescp

--0000000000000765de05806de3c2
Content-Type: text/html; charset="UTF-8"

<div dir="auto">Muiescp</div>

--0000000000000765de05806de3c2--

Muiescp'''

soup = BeautifulSoup(email, 'lxml')
subject = re.search(r'Subject: (.*)', email).group(1)
email_from = re.search(r'From: (.*)', email).group(1)
message = soup.find('div')
print(subject)
print(email_from)
print((message.text))
Output:
Muie Wind Bullet <[email protected]> Muiescp
Reply


Messages In This Thread
Read emails python 3. - by negru555 - Jan-26-2019, 07:27 AM
RE: Read emails python 3. - by Larz60+ - Jan-26-2019, 11:43 AM
RE: Read emails python 3. - by negru555 - Jan-26-2019, 06:34 PM
RE: Read emails python 3. - by Larz60+ - Jan-26-2019, 08:26 PM
RE: Read emails python 3. - by negru555 - Jan-27-2019, 10:21 AM
RE: Read emails python 3. - by snippsat - Jan-27-2019, 12:41 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020