2008-11-24 4 views
10

tldr: Kann mir jemand zeigen, wie man dieses Python iMAP Beispiel richtig formatiert, damit es funktioniert?Richtig formatiertes Beispiel für Python-iMAP-E-Mail-Zugriff?

von 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() 

per E-Mail Unter der Annahme, "[email protected]" und das Passwort "password", wie soll diese aussehen? Ich versuchte M.login(getpass.getuser([email protected]), getpass.getpass(password)) und es Zeitüberschreitung. Vollständig neu hier, so ist es sehr wahrscheinlich, dass ich etwas offensichtlich übersehen habe (wie zum Beispiel ein iMAP-Objekt erstellen? Nicht sicher).

+2

Code auswählen, drücken Sie Strg + K – jfs

Antwort

2

Haben Sie vergessen, den IMAP-Host und -Port anzugeben? Verwenden Sie etwas in der Art von:

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

oder

M = imaplib.IMAP4_SSL() 
M.open('imap.gmail.com') 
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") 

Der Rest des Codes richtig zu sein scheint.

+2

Nur um anderen Zeit zu retten, wer könnte das sehen ... es ist "IMAP4_SSL" nicht nur "IMAP_SSL". – Seaux

+0

@ocdcoder: schöner Fang, danke. – tzot

11

Hier ist ein Skript, das ich verwendet habe, um Logwatch Informationen aus meinem Postfach zu greifen. 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() 
+4

Caveat-Programmierer: Verwendung von Python-Modul "rfc822" ist seit Python 2.3 veraltet. (Quelle: http://docs.python.org/2.6/library/rfc822.html) Verwenden Sie stattdessen das E-Mail-Modul. –

0

Statt M.login(getpass.getuser([email protected]), getpass.getpass(password)) Sie müssen M.login('[email protected]', 'password') verwenden, das heißt blanke Saiten (oder besser, Variablen, welche sie enthalten). Ihr Versuch hätte eigentlich gar nicht funktionieren sollen, denn getpass 's getuser nimmt keine Argumente, sondern gibt nur den Benutzernamen zurück. Und [email protected] ist nicht einmal ein gültiger Variablenname (Sie haben es nicht in Anführungszeichen gesetzt) ​​...