【问题标题】:"SELECT command error BAD" during IMAP call to Gmail在 IMAP 调用 Gmail 期间出现“SELECT command error BAD”
【发布时间】:2017-09-03 19:45:34
【问题描述】:

我想通过 Google 帐户使用 IMAP 获取 G-mail。 所以我找到了一些如何使用 IMAP 的基本代码。 我使用我的 Google 帐户运行它。但我得到一些错误。 我不知道函数或参数的作用,所以如果你有 gmail 帐户并且知道函数的作用,请帮助我。

这是我的代码,我隐藏了我的帐户信息,因此真实帐户与我的不同。

#!/usr/bin/env python

import sys
import imaplib
import getpass
import email
import email.header
import datetime
EMAIL_ACCOUNT = "id@gmail.com"
EMAIL_PASSWORD = "password"
# Use 'INBOX' to read inbox.  Note that whatever folder is specified,
# after successfully running this script all emails in that folder
# will be marked as read.
EMAIL_FOLDER = "Top Secret/PRISM Documents"
def process_mailbox(M):

rv, data = M.search(None, "ALL")
if rv != 'OK':
    print("No messages found!")
    return
    for num in data[0].split():
        rv, data = M.fetch(num, '(RFC822)')
        if rv != 'OK':
            print("ERROR getting message", num)
            return
        msg = email.message_from_bytes(data[0][1])
        hdr = 
        email.header.make_header(email.header.decode_header(msg['Subject']))
        subject = str(hdr)
        print('Message %s: %s' % (num, subject))
        print('Raw Date:', msg['Date'])
        # Now convert to local date-time
        date_tuple = email.utils.parsedate_tz(msg['Date'])
        if date_tuple:
            local_date = datetime.datetime.fromtimestamp(
                email.utils.mktime_tz(date_tuple))
            print ("Local Date:", \
                local_date.strftime("%a, %d %b %Y %H:%M:%S"))
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
    rv, data = M.login(EMAIL_ACCOUNT, EMAIL_PASSWORD)
except imaplib.IMAP4.error:
    print ("LOGIN FAILED!!! ")
    sys.exit(1)
print(rv, data)
rv, mailboxes = M.list()
if rv == 'OK':
    print("Mailboxes:")
    print(mailboxes)
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
    print("Processing mailbox...\n")
    process_mailbox(M)
    M.close()
else:
    print("ERROR: Unable to open mailbox ", rv)
M.logout()

这是错误结果。

这是文本错误。

C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/user/PycharmProjects/Server/imap.py
OK [b'testpopteam2@gmail.com authenticated (Success)']
Mailboxes:
[b'(\\HasNoChildren) "/" "INBOX"', b'(\\HasChildren \\Noselect) "/" "[Gmail]"', b'(\\Flagged \\HasNoChildren) "/" "[Gmail]/&vMTUXNO4ycDVaA-"', b'(\\HasNoChildren \\Sent) "/" "[Gmail]/&vPSwuNO4ycDVaA-"', b'(\\HasNoChildren \\Junk) "/" "[Gmail]/&wqTTONVo-"', b'(\\Drafts \\HasNoChildren) "/" "[Gmail]/&x4TC3Lz0rQDVaA-"', b'(\\All \\HasNoChildren) "/" "[Gmail]/&yATMtLz0rQDVaA-"', b'(\\HasNoChildren \\Important) "/" "[Gmail]/&yRHGlA-"', b'(\\HasNoChildren \\Trash) "/" "[Gmail]/&1zTJwNG1-"']
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/Server/imap.py", line 73, in <module>
    rv, data = M.select(EMAIL_FOLDER)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 737, in select
    typ, dat = self._simple_command(name, mailbox)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 1188, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 1019, 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']

Process finished with exit code 1

【问题讨论】:

  • 我将错误粘贴为文本,但由于路径的原因很难弄清楚。
  • 如果您更改您的EMAIL_FOLDER 以匹配mailboxes 列表中的值之一,这会改变吗?
  • ...btw,看起来你的语法高亮是错误的——def process_mailbox(M): 之后没有缩进。将来,请考虑选择您的整个代码并使用{} 按钮缩进要视为代码的区域。
  • 哦!谢谢!我更改了 EMAIL_FOLDER 值,终于收到邮件了!
  • 我将使用 { } 按钮下一个问题。谢谢你的建议。

标签: python imap gmail-imap imaplib


【解决方案1】:

来电

M.select(EMAIL_FOLDER)

不包括从以下位置返回的任何邮箱:

rv, mailboxes = M.list()

确保邮箱名称有效。

【讨论】:

  • 另外,我认为 imaplib 有一个错误,即如果文件夹名称中有空格,它不会引用文件夹名称,因此可能需要手动完成。
猜你喜欢
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 2014-11-03
  • 2015-11-18
相关资源
最近更新 更多