更新:我的代码在 python 2.6.5 下运行,但在 python 3 下运行(我使用的是 3.4.1).
Update: my code works under python 2.6.5 but not python 3 (I'm using 3.4.1).
我无法在所有邮件"或已发送邮件"文件夹中搜索邮件 - 我遇到了异常:
I'm unable to search for messages in the "All Mail" or "Sent Mail" folders - I get an exception:
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")
也不起作用.
但是从收件箱中读取是有效的:
But reading from the inbox works:
import imaplib
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail@gmail.com","mypassword")
m.select("inbox")
...
我使用了 mail.list() 命令来验证文件夹名称是否正确:
I used the mail.list() command to verify the folder names are correct:
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 -Gmail 中存档/所有邮件的正确文件夹名称是什么?
I'm following the solutions from these questions, but they don't work for me:
imaplib - What is the correct folder name for Archive/All Mail in Gmail?
我无法在 Gmail 中搜索已发送的电子邮件使用 Python
这是一个完整的示例程序,不适用于 Python 3:
Here is a complete sample program that doesn't work on 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()
正如 这个答案:
尝试使用 m.select('"[Gmail]/All Mail"'),以便传输双引号.我怀疑 imaplib 没有正确引用字符串,因此服务器得到了两个参数:[Gmail]/All 和 Mail.
Try using m.select('"[Gmail]/All Mail"'), so that the double quotes get transmitted. I suspect imaplib is not properly quoting the string, so the server gets what looks like two arguments: [Gmail]/All, and 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()
这篇关于无法从收件箱以外的任何文件夹中检索 gmail 邮件(Python3 问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!