Extracting a body of an email using [Python 3.6]

361 views Asked by At

I have an inbox and I need to write a code that triggers every time a new message arrives, and extracts a body of that email.

I have this code so far:

import poplib, email
from email import parser

pop_conn = poplib.POP3_SSL('pop.gmail.com')
email_user = '[email protected]'
email_pass = 'password'

pop_conn.user(email_user)
pop_conn.pass_(email_pass)

#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, 
len(pop_conn.list()[1]) + 1)]

#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print(message.get_payload(None, True))

pop_conn.quit()

The error I am getting is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-2a08d6a8ccf9> in <module>()
  2 #Parse message intom an email object:
  3 #messages = [email.message_from_bytes(mssg) for mssg in messages]
----> 4 messages = [parser.Parser().parsestr(mssg) for mssg in messages]
  5 for message in messages:
  6 #    print(message['body'])

<ipython-input-6-2a08d6a8ccf9> in <listcomp>(.0)
  2 #Parse message intom an email object:
  3 #messages = [email.message_from_bytes(mssg) for mssg in messages]
----> 4 messages = [parser.Parser().parsestr(mssg) for mssg in messages]
  5 for message in messages:
  6 #    print(message['body'])

/Applications/anaconda3/lib/python3.6/email/parser.py in parsestr(self, text, headersonly)
 66         the file.
 67         """
---> 68         return self.parse(StringIO(text), headersonly=headersonly)
 69 
 70 

TypeError: initial_value must be str or None, not tuple

TypeError: initial_value must be str or None, not tuple

Why am I getting a tuple? How do I extract a body from the messages[n]?

1

There are 1 answers

0
tripleee On

The return value from retr() is a tuple. The second value in the tuple is a list of lines which comprise the actual message. See the Python poplib documentation for details.

# Get messages from server
popped = ['\n'.join(pop_conn.retr(i)[1])
    for i in range(1, len(pop_conn.list()[1]) + 1)]

# Parse message into an email object
messages = [parser.Parser().parsestr(mssg) for mssg in popped]

Notice also how we avoid reusing the same variable name for objects of different types.