2017-04-08 1 views
0

IMAP을 사용하여 Google 계정으로 G 메일을 보내려고합니다. 그래서 IMAP을 사용하는 기본 코드를 발견했습니다. 그리고 Google 계정으로 실행합니다. 하지만 몇 가지 오류가 발생합니다. 함수 또는 매개 변수의 역할을 모르므로 gmail 계정이 있고 함수의 역할을 알고 있다면 도와주세요.Gmail에 대한 IMAP 호출 중 "SELECT 명령 오류 BAD"가 발생했습니다.

이것은 내 코드이며 계정 정보를 숨겨 실제 계정이 다른 것과 다릅니다.

#!/usr/bin/env python 

import sys 
import imaplib 
import getpass 
import email 
import email.header 
import datetime 
EMAIL_ACCOUNT = "[email protected]" 
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() 

이것은 오류 결과입니다. enter image description here

이것은 텍스트 오류입니다.

rv, mailboxes = M.list() 

사서함 이름이 유효한지 확인하십시오에서 사서함의 포함되지

C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/user/PycharmProjects/Server/imap.py 
OK [b'[email protected] 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 
+0

텍스트로 오류를 붙여 넣지 만 경로로 인해 오류를 찾아 내기가 어렵습니다. – Jung

+0

'EMAIL_FOLDER'을'mailboxes' 목록에있는 값 중 하나와 일치하도록 바꾸면 그 것이 변경됩니까? –

+0

... btw, 구문 강조가 잘못되어있는 것처럼 보입니다. def process_mailbox (M) :'뒤에 들여 쓰기가 없습니다. 앞으로 * 전체 * 코드를 선택하고'{}'버튼을 사용하여 코드로 간주 할 영역을 들여 쓰기하는 것이 좋습니다. –

답변

0

호출

M.select(EMAIL_FOLDER) 

이 돌아왔다.

+1

또한 imaplib에는 공백이 있으면 폴더 이름을 인용하지 않는 버그가있어서 수동으로 수행해야 할 수도 있습니다. – Max

관련 문제