更新:我的代码在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("[email protected]","mypassword")
m.select("[Gmail]/All Mail")
使用m.select("[Gmail]/Sent Mail")
也不起作用。
但是从收件箱中读取作品:
import imaplib
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("[email protected]","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("[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()
引发以下异常:
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("[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()
参考方案
正如this answer中提到的:
尝试使用m.select('“[[Gmail] / All Mail”')),以使双引号得到传输。
我怀疑imaplib不能正确引用该字符串,因此服务器获取的外观类似于两个参数:[Gmail] / All和Mail。
它可以在python v3.4.1
中工作
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()
Python:BeautifulSoup-根据名称属性获取属性值 - python我想根据属性名称打印属性值,例如<META NAME="City" content="Austin"> 我想做这样的事情soup = BeautifulSoup(f) //f is some HTML containing the above meta tag for meta_tag in soup(…
您如何在列表内部调用一个字符串位置? - python我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
Python-Mailchimp批处理PUT请求 - python我正在尝试对Mailchimp API使用批处理功能。我当前的设置是这样的operations = [] for idx, row in users_df.iterows(): payload = { 'email': row['email'], 'last_updated': row['…
Python-将字节数组转换为JSON格式 - python我想将bytes数组转换为JSON格式。这是我的来源:my_bytes_value = b'[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\', \'LogoType\&…