tldr: Can someone show me how to properly format this Python iMAP example so it works?
from https://docs.python.org/2.4/lib/imap4-example.html
import getpass, imaplib M = imaplib.IMAP4() M.login(getpass.getuser(), getpass.getpass()) M.select() typ, data = M.search(None, 'ALL') for num in data[0].split(): typ, data = M.fetch(num, '(RFC822)') print 'Message %s\n%s\n' % (num, data[0][1]) M.close() M.logout()
Assuming my email is "email@gmail.com" and the password is "password," how should this look? I tried M.login(getpass.getuser(email@gmail.com), getpass.getpass(password))
and it timed out. Complete newb here, so it's very likely I missed something obvious (like creating an iMAP object first? Not sure).
The rest of your code seems correct.
Instead of
M.login(getpass.getuser(email@gmail.com), getpass.getpass(password))
you need to useM.login('email@gmail.com', 'password')
, i.e. plain strings (or better, variables containing them). Your attempt actually shouldn't have worked at all, sincegetpass
'sgetuser
doesn't take arguments but merely returns the user login name. Andemail@gmail.com
isn't even a valid variable name (you didn't put it into quotes)...Here is a script I used to use to grab logwatch info from my mailbox. Presented at LFNW 2008 -
Did you forget to specify the IMAP host and port? Use something to the effect of:
or,