Kann nicht zum abrufen von gmail-Nachrichten aus jedem Ordner außer Posteingang (Python ist3 Ausgabe)

Update: mein code funktioniert unter python 2.6.5, aber nicht python 3 (ich bin mit 3.4.1).

Ich bin nicht in der Lage, um die Suche für Nachrichten, die in den "Alle Nachrichten" oder "Gesendete Nachrichten" Ordner - bekomme ich eine exception:

imaplib.error: SELECT command error: BAD [b'Could not parse command']

mein code:

import imaplib
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("[email protected]","mypassword")
m.select("[Gmail]/All Mail")

mit m.select("[Gmail]/Sent Mail") funktioniert auch nicht.

Aber das Lesen aus dem Posteingang funktioniert:

import imaplib
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("[email protected]","mypassword")
m.select("inbox")
...

Benutzte ich die mail.list () - Befehl überprüfen Sie die Ordner-und Dateinamen korrekt sind:

b'(\\HasNoChildren) "/" "INBOX"', 
b'(\\Noselect \\HasChildren) "/" "[Gmail]"',
b'(\\HasNoChildren \\All) "/" "[Gmail]/All Mail"', 
b'(\\HasNoChildren \\Drafts) "/" "[Gmail]/Drafts"', 
b'(\\HasNoChildren \\Important) "/" "[Gmail]/Important"', 
b'(\\HasNoChildren \\Sent) "/" "[Gmail]/Sent Mail"', 
b'(\\HasNoChildren \\Junk) "/" "[Gmail]/Spam"', 
b'(\\HasNoChildren \\Flagged) "/" "[Gmail]/Starred"', 
b'(\\HasNoChildren \\Trash) "/" "[Gmail]/Trash"'

Bin ich nach den Lösungen dieser Fragen, aber Sie arbeiten nicht für mich:
imaplib - Was ist der richtige Ordner für Archiv/Alle E-Mails in Gmail?

Ich Suche nicht gesendeten E-Mails in Gmail mit Python

Hier ist ein vollständiges Beispiel-Programm, das funktioniert nicht auf Python 3:

import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("[email protected]","mypassword")
m.select("[Gmail]/All Mail")

result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
    for num in data[0].split():
        result, data = m.uid('fetch', num, '(RFC822)')
        if result == 'OK':
            email_message = email.message_from_bytes(data[0][1])    # raw email text including headers
            print('From:' + email_message['From'])

m.close()
m.logout()

Folgende exception geworfen wird:

Traceback (most recent call last):
File "./eport3.py", line 9, in <module>
m.select("[Gmail]/All Mail")
File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 682, in select
typ, dat = self._simple_command(name, mailbox)
File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 1134, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 965, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SELECT command error: BAD [b'Could not parse command']

Hier ist der entsprechende Python 2-version , das funktioniert:

import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("[email protected]","mypassword")
m.select("[Gmail]/All Mail")

result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
    for num in data[0].split():
        result, data = m.uid('fetch', num, '(RFC822)')
        if result == 'OK':
            email_message = email.message_from_string(data[0][1])    # raw email text including headers
            print 'From:' + email_message['From']

m.close()
m.logout()

InformationsquelleAutor Randy Dellinger | 2014-08-07

Schreibe einen Kommentar