poplib and email module will not reloop through a message if it has alread read it

896 views Asked by At

I'm currently trying to write a script that gets messages from my gmail account but I'm noticing a problem. If poplib loops through a message in my inbox it will never loop through it again. Here is my code

import poplib, string, email

user = "[email protected]"
password = "password"

message = ""
mail = poplib.POP3_SSL('pop.gmail.com')
mail.user(user)
mail.pass_(password)

iMessageCount = len(mail.list()[1])
message = ""
msg = mail.retr(iMessageCount)
str = string.join(msg[1], "\n")
frmMail = email.message_from_string(str)

for part in frmMail.walk():
    if part.get_content_type() == "text/plain":
        print part.get_payload()

mail.quit()

Every time I run this script it goes to the next newest email and just skips over the email that was shown last time it was run.

1

There are 1 answers

0
mhawke On

Retrieving a message with mail.retr() sets the seen flag for that message and so it will no longer be visible via mail.list() after mail.quit() has been called.

If you do not call mail.quit() your messages will remain unseen.

BTW, gmail supports IMAP which is generally better than POP3.