![](/img/trans.png)
[英]Use IMAP4 in python to move mail from a mailbox to another mailbox
[英]How to select a specific mailbox from IMAP server?
我只想選擇郵箱Folder1並檢查是否有任何子目錄。 我已經嘗試了以下代碼:
svr = imaplib.IMAP4_SSL(imap_address)
svr.login(user, pwd)
svr.select('inbox') <<<<<<<<<<<<<<<<<
rv, data = svr.search(None, "ALL")
test, folders = svr.list('""', '*')
print(folders)
我認為將“收件箱”更改為“文件夾1”(用箭頭指示的語句)將選擇“ Folder1”,然后可以檢索子目錄。 但是什么也沒發生,仍然顯示與“收件箱”相同的結果。
有人可以幫助我了解我在這里做錯了什么。
由於我不知道文件夾的名稱,因此嘗試了另一種方法。 我將首先收集根目錄中的所有文件夾,然后一個個解析它們以檢查是否存在任何子目錄。
root_folders = []
svr = imaplib.IMAP4_SSL(imap_address)
svr.login(user, pwd)
svr.select('inbox')
response, folders = svr.list('""', '*')
def parse_mailbox(data):
flags, b, c = data.partition(' ')
separator, b, name = c.partition(' ')
return flags, separator.replace('"', ''), name.replace('"', '')
def subdirectory(folder):
#For directories 'Deleted Items', 'Sent Items', etc. with whitespaces,
#the name of the directory needs to be passed with double quotes, hence '"' + name + '"'
test, folders = obj.list('""','"' + name+ '/*"')
if(folders is not None):
print('Subdirectory exists') # you can also call parse_mailbox to find the name of sub-directory
for mbox in folders:
flags, separator, name = parse_mailbox(bytes.decode(mbox))
fmt = '{0} : [Flags = {1}; Separator = {2}'
if len(name.split('/')) > 1:
continue
else:
root_folders.append(name)
for folder in root_folders:
subdirectory(folder)
雖然這是我腳本中的量身定制的代碼,但這應該是提出的問題的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.