简体   繁体   中英

python script to download attachments in email having keywords in subject line

import getpass, poplib, email, parse
from poplib  import POP3
user = 'rnandipati@qwerty.com' 
M = poplib.POP3_SSL('outlook.office365.com', '995') 
M.user(user) 
M.pass_('R7!')
numMessages = len(M.list()[1])
print ("You have %d messages." % (numMessages))
print ("Message List:")

M.quit()

I have the above code that gives the number of messages in my email. I want to download attachments from the messages that have "hello" in the subject line.

What i have tried:

 for mList in range(numMessages):
        for msg in M.retr(mList+1)[1]:
            if msg.startswith('Subject'):
                print(msg)
                break

I have looked at a lot of examples online and really need help with this. I am also a newbie in such scripting.

Thank You.

Quoting the documentation:

POP3.retr( which )

Retrieve whole message number which , and set its seen flag. Result is in form (response, ['line', ...], octets).

So M.retr(mList+1)[1] is a list of lines.

You are iterating a list of strings, which can be unicode ou bytes strings.

The documentation doesn't say much about which type of string you get, but, if you are using Python 3, I make the assumption that it is unicode string.

So: msg.startswith("Subject:") should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM