Properly formatted example for Python iMAP email access?
Asked Answered
J

4

10

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 protected]" and the password is "password," how should this look? I tried M.login(getpass.getuser([email protected]), 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).

Jehu answered 24/11, 2008 at 20:31 Comment(0)
P
11
import imaplib

# you want to connect to a server; specify which server
server= imaplib.IMAP4_SSL('imap.googlemail.com')
# after connecting, tell the server who you are
server.login('[email protected]', 'password')
# this will show you a list of available folders
# possibly your Inbox is called INBOX, but check the list of mailboxes
code, mailboxen= server.list()
print mailboxen
# if it's called INBOX, then…
server.select("INBOX")

The rest of your code seems correct.

Phalarope answered 24/11, 2008 at 22:18 Comment(1)
Just to save others time who might see this...it's "IMAP4_SSL" not just "IMAP_SSL".Featherbedding
M
11

Here is a script I used to use to grab logwatch info from my mailbox. Presented at LFNW 2008 -

#!/usr/bin/env python

''' Utility to scan my mailbox for new mesages from Logwatch on systems and then
    grab useful info from the message and output a summary page.

    by Brian C. Lane <[email protected]>
'''
import os, sys, imaplib, rfc822, re, StringIO

server  ='mail.brianlane.com'
username='yourusername'
password='yourpassword'

M = imaplib.IMAP4_SSL(server)
M.login(username, password)
M.select()
typ, data = M.search(None, '(UNSEEN SUBJECT "Logwatch")')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
#   print 'Message %s\n%s\n' % (num, data[0][1])

    match = re.search(  "^(Users logging in.*?)^\w",
                        data[0][1],
                        re.MULTILINE|re.DOTALL )
    if match:
        file = StringIO.StringIO(data[0][1])
        message = rfc822.Message(file)
        print message['from']
        print match.group(1).strip()
        print '----'

M.close()
M.logout()
Milburn answered 25/11, 2008 at 5:42 Comment(1)
Caveat programmer: use of Python module "rfc822" has been deprecated since Python 2.3. (Source: docs.python.org/2.6/library/rfc822.html) Consider using email module, instead.Polak
B
2

Did you forget to specify the IMAP host and port? Use something to the effect of:

M = imaplib.IMAP4_SSL( 'imap.gmail.com' )

or,

M = imaplib.IMAP4_SSL()
M.open( 'imap.gmail.com' )
Brittle answered 24/11, 2008 at 20:39 Comment(0)
W
0

Instead of M.login(getpass.getuser([email protected]), getpass.getpass(password)) you need to use M.login('[email protected]', 'password'), i.e. plain strings (or better, variables containing them). Your attempt actually shouldn't have worked at all, since getpass's getuser doesn't take arguments but merely returns the user login name. And [email protected] isn't even a valid variable name (you didn't put it into quotes)...

Wieldy answered 19/4, 2015 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.