1

I'm using this code to check for new mails with a 10 seconds delay.

import poplib
from email import parser
import time

def seeit(): 
    pop_conn = poplib.POP3_SSL('pop.gmail.com')
    pop_conn.user('xxx')
    pop_conn.pass_('xx')
    #Get messages from server:
    messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
    # Concat message pieces:a
    messages = ["\n".join(mssg[1]) for mssg in messages]
    #Parse message intom an email object:
    messages = [parser.Parser().parsestr(mssg) for mssg in messages]
    for message in messages:
        print message['subject']
    pop_conn.quit()


starttime=time.time()

while True:
    k=10.0
    print "tick"
    seeit()
    time.sleep(k - ((time.time() - starttime) % k))

How can i retrieve the email body without headers?

Nurjan
  • 5,889
  • 5
  • 34
  • 54
  • Do you get the messages without any errors? – Nurjan Oct 28 '16 at 09:01
  • yes. currently it displays the 'subject' – Ramith Hettiarachchi Oct 28 '16 at 09:02
  • Can you print message bodies? You want to print message bodies without subjects? Right? – Nurjan Oct 28 '16 at 09:05
  • actually i want to print both the subject and the body. – Ramith Hettiarachchi Oct 28 '16 at 09:06
  • The code you posted does not run. Please [edit] your question to provide runnable code. Indentation is significant in Python, so you will want to make sure the indentation in your question matches what you actually run. Usually, copy/paste the code, select the pasted block, and press ctrl-K to have it properly formatted for Stack Overflow. There will be detailed Markdown help on the right while you are editing. – tripleee Oct 28 '16 at 09:46

1 Answers1

0

Look at the method get_payload() method from the email package.

Try this after you get the messages from server:

import email

...
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
...

for i in messages:
    mssg = email.message_from_string(i)
    print(mssg.get_payload())
Nurjan
  • 5,889
  • 5
  • 34
  • 54