0

First post after searching all over the site.

I am trying to search the body of emails in a outlook mailbox for email addresses. I am using Imap_tools, MailParser and Beautiful Soup. I need to get any email address present in the body of the email to use in another section of the script I'm writing. Maybe I'm doing too much but need this to work.

This is what I have so far.

with MailBox('outlook.office365.com').xoauth2('MAILBOX@domain.com', result['access_token'], 'INBOX') as mailbox:
        for msg in mailbox.fetch(A(seen= True, subject='SUBJECT', from_= 'EMAIL')):
            #to validate it's fetching the correct emails
            print(msg.date_str, msg.subject) 
                email_message = mailparser.parse_from_file_obj(msg.obj)
                soup = BeautifulSoup(email_message.body, "html.parser")
                print(soup)
                text = soup.get_text()
                # Find all email addresses in the body of the email
                email = re.findall(r'[\w\.-]+@[\w\.-]+', text)
                print(email)
                email = email[0]

This is the error I'm getting

Traceback (most recent call last):
  File ".\testServPrinc.py", line 55, in <module>
    email_message = mailparser.parse_from_file_obj(msg.obj)
  File AppData\Roaming\Python\Python38\s
r.py", line 66, in parse_from_file_obj
    return MailParser.from_file_obj(fp)
  File \AppData\Roaming\Python\Python38\s
r.py", line 166, in from_file_obj
    s = fp.read()
AttributeError: 'Message' object has no attribute 'read'

All help appreciated.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
litenchi
  • 1
  • 1
  • What is msg.obj? Is it actually a file object? Are you sure parse_from_file_obj() is the right thing to be using? – Max Nov 18 '22 at 16:12
  • 1
    It was not. I actually have since figured this out. Ended up using msg.html and removing mailparser altogether. I guess I just needed that extra 3 hours to make it a clean 3 days & 3 hours to figure this problem out. Thanks for trying to help though! – litenchi Nov 18 '22 at 20:42

1 Answers1

0
body = msg.text or msg.html 

There may be several mail representation forms in a letter

Vladimir
  • 6,162
  • 2
  • 32
  • 36