IMAP fetch() returns command error: BAD [b' Command Argument Error. 12']

霸气de小男生 提交于 2021-02-10 04:58:13

问题


I'm having trouble finding examples/troubleshooting tips online, and am not quite sure that I'm interpreting the documentation correctly. Any assistance would be greatly appreciated.

I'm connecting to an e-mail server, and want to read the e-mail subjects, and bodies. I first make my connection like so:

import imaplib
c = imaplib.IMAP4_SSL(hostname, port)
c.login(username, password)

foldername = 'INBOX/SSR'
c.select(str.encode(foldername), readonly = True)

today = datetime.date.today().strftime('%d-%b-%Y')
searchcriteria = '(SENTON '{}')'.format(today)
typ, msg_ids = c.search(None, searchcriteria)
msg_ids = [s.decode('ascii') for s in msg_ids]

for idnumber in msg_ids: print(c.fetch(idnumber, "(BODY.PEEK[HEADER])"))

The code and works and output looks as expected, up until the last line, at which point, I get

imaplib.error: FETCH command error: BAD [b' Command Argument Error. 12']

My line of thought, and subsequent testing examined the following possible issues:

  1. bytes vs. string. I converted input back to bytes, but the error remained constant
  2. improper syntax: I tried other commands, such as BODY, SUBJECT, and ENVELOPE but still got the same message.

I'm not sure how to interpret the error, and don't really know where to start. Referencing http://tools.ietf.org/html/rfc3501.html from pp. 102+, I noticed that the values are labeled differently, but don't understand what the issue is with my implementation. How should I interpret the error? What is wrong with my syntax?

P.S. Correct me if I'm wrong, but the c.search shouldn't change my directory, yes? As in, by selecting foldername, I "navigate" to the selected folder, but just searching only returns values and shouldn't change my location?


回答1:


I encountered the same problem while I tried to list or select a new mailbox - BAD [b' Command Argument Error. 12'], in my case, it didn't work with “Sent Box”, but it worked well with “Outbox”, so the space symbol is the point.

So it worked with c.select('"{}"'.format("Sent Box")...

Hope this information could help you.




回答2:


Your last line is not correct msg_ids = [s.decode('ascii') for s in msg_ids]

msg_ids is a list with bytes string, not with elements of a list - example: [b'123 124 125']

Change the last line into msg_ids = msg_ids[0].split(b' ') and it will work as expected.



来源:https://stackoverflow.com/questions/26981650/imap-fetch-returns-command-error-bad-b-command-argument-error-12

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!