1

I've been trying to print email body using python(using imap_tools lib)but it's not really working for me. here is the code snippet.

from imap_tools import MailBox, A, AND, OR, NOT
with MailBox('outlook.office365.com').login('test@test.com', 'test') as mailbox:
    for msg in mailbox.fetch(A(seen=False)):
        print(msg.text)

but when I print the subject using msg.subject, it's working fine. I tried using msg.html and here is the output.

<body lang="EN-IN" link="blue" vlink="#954F72" style="word-wrap:break-word"><div class="WordSection1"><p class="MsoNormal">Hi, this is a test email to store body in file<o:p></o:p></p></div></body></html>

note: this is not the whole HTML output, but this is to show that the email has some data in the body. Can anyone help me solve this problem?

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • What do you think is wrong with that body? It looks okay to me. – Max Jan 17 '23 at 16:09
  • Not every message necessarily has both a .text and a .html body, the sender client can choose to include one, the other, or both. Technically 'neither' is an option, but rarely seen in practice. – Max Jan 17 '23 at 16:26
  • @Max It contains many HTML tags and other CSS components which I don't need. i am looking for a str which is the body – Mr. Annonymous Jan 17 '23 at 16:55
  • Then if the message doesn't already contain a text part, you'll need to remove them yourself. There are several suggestions here: https://stackoverflow.com/questions/753052/strip-html-from-strings-in-python – Max Jan 17 '23 at 17:28
  • @Max the body is in text only, no HTML content is present in the body. – Mr. Annonymous Jan 17 '23 at 18:42
  • 1
    Obviously it is not text-only. It may be very lightly styled HTML, but those are the normal tags you get from Outlook generated messages (“MsoNormal == microsoft office normal”). It may not have images, but it _is_ HTML. Either ask your sender to adjust their settings to actually text only, or strip them. – Max Jan 17 '23 at 18:57

0 Answers0