showkey
showkey

Reputation: 348

How to copy email in INBOX into important mailbox with imaplib?

I am in python3.4 .

import imaplib
import email
user="xxxx"
password="yyyy"
con=imaplib.IMAP4_SSL('imap.gmail.com')
con.login(user,password)
con.list()

('OK', [b'(\\HasNoChildren) "/" "INBOX"', b'(\\Noselect \\HasChildren) "/" "[Gma il]"', b'(\\HasNoChildren \\Junk) "/" "[Gmail]/&V4NXPpCuTvY-"', b'(\\HasNoChildr en \\Trash) "/" "[Gmail]/&XfJSIJZkkK5O9g-"', b'(\\HasNoChildren \\Flagged) "/" " [Gmail]/&XfJSoGYfaAc-"', b'(\\HasNoChildren \\Sent) "/" "[Gmail]/&XfJT0ZCuTvY-"' , b'(\\HasNoChildren \\All) "/" "[Gmail]/&YkBnCZCuTvY-"', b'(\\HasNoChildren \\D rafts) "/" "[Gmail]/&g0l6Pw-"', b'(\\HasNoChildren \\Important) "/" "[Gmail]/&kc 2JgQ-"'])

I want to copy all the email in INBOX into my Important mailbox.

con.select("INBOX")
typ,data=con.search(None,'ALL')
num=data[0].split()
for item in num:
    con.copy(item,"[Gmail]/&kc2JgQ-]")

error message: ('NO', [b'[TRYCREATE] No folder [Gmail]/\xe9\x87\x8d\xe8\xa6\x81] (Failure)'])

for item in num:
    con.copy(item,"[Gmail]/Important")

error message: ('NO', [b'[TRYCREATE] No folder [Gmail]/Important (Failure)'])

for item in num:
    con.copy(item,"\\Important")

error message: imaplib.error: COPY command error: BAD [b'Could not parse command']

How to copy email in INBOX into important mailbox with imaplib?

Upvotes: 0

Views: 2097

Answers (1)

abarnert
abarnert

Reputation: 365657

In your code:

con.copy(item,"[Gmail]/&kc2JgQ-]")

… you've got a stray ] on the end. The string you copied it from looks like this:

"[Gmail]/&kc2JgQ-"

Upvotes: 1

Related Questions