无法从收件箱以外的任何文件夹中检索gmail邮件(Python3问题)

时间:2014-08-07 15:27:46

标签: python email python-3.x gmail imaplib

更新:我的代码在python 2.6.5下运行,但不在python 3下运行(我使用的是3.4.1)。

我无法在"所有邮件"中搜索邮件。或者"发送邮件"文件夹 - 我得到一个例外:

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

我的代码:

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

使用m.select("[Gmail]/Sent Mail")也不起作用。

但是从收件箱中读取信息可以:

import imaplib
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail@gmail.com","mypassword")
m.select("inbox")
...

我使用mail.list()命令验证文件夹名称是否正确:

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"'

我遵循这些问题的解决方案,但他们不为我工作:
imaplib - What is the correct folder name for Archive/All Mail in Gmail?

I cannot search sent emails in Gmail with Python

以下是不适用于Python 3的完整示例程序

import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail@gmail.com","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()

抛出以下异常:

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']

这里有相应的Python 2版本可以使用

import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail@gmail.com","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()

1 个答案:

答案 0 :(得分:35)

正如this answer中提到的那样:

  

尝试使用m.select('[“[Gmail] /所有邮件”),以便传输双引号。   我怀疑imaplib没有正确引用该字符串,因此服务器获得了两个参数:[Gmail] / All和Mail。

它适用于python v3.4.1

import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail@gmail.com","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()