这是我的代码:
import imaplib
from email.parser import HeaderParser
conn = imaplib.IMAP4_SSL('imap.gmail.com')
conn.login('example@gmail.com', 'password')
conn.select()
conn.search(None, 'ALL')
data = conn.fetch('1', '(BODY[HEADER])')
header_data = data[1][0][1]
newdata = header_data.decode('utf-8')
parser = HeaderParser()
msg = parser.parsestr(newdata)
print (msg.keys())
labels = conn.fetch('1:100', (X-GM-LABELS))
print (labels)
当我打印出标签时,它会打印出每个电子邮件标题中的x-GM标签(这是电子邮件所在的文件夹)。但是,它只打印出重要信息,并发送了x-gm标签。我在1:100范围内的收件箱中有大约60封电子邮件。那么,为什么不打印出收件箱呢?它只是打印()。这是打印输出,所以你可以正确看到它。
'OK', [b'50 (X-GM-LABELS ())', b'51 (X-GM-LABELS ())', b'52 (X-GM-LABELS ())', b'53 (X-GM-LABELS ())', b'54 (X-GM-LABELS ())', b'55 (X-GM-LABELS ())', b'56 (X-GM-LABELS ())', b'57 (X-GM-LABELS ())', b'58 (X-GM-LABELS ("\\\\Important"))', b'59 (X-GM-LABELS ())', b'60 (X-GM-LABELS ())', b'61 (X-GM-LABELS ())', b'62 (X-GM-LABELS ())', b'63 (X-GM-LABELS ())', b'64 (X-GM-LABELS ())', b'65 (X-GM-LABELS ("\\\\Important"))', b'66 (X-GM-LABELS ())', b'67 (X-GM-LABELS ())', b'68 (X-GM-LABELS ())', b'69 (X-GM-LABELS ())', b'70 (X-GM-LABELS ())', b'71 (X-GM-LABELS ())', b'72 (X-GM-LABELS ())', b'73 (X-GM-LABELS ())', b'74 (X-GM-LABELS ("\\\\Important"))', b'75 (X-GM-LABELS ("\\\\Important"))', b'76 (X-GM-LABELS ())', b'77 (X-GM-LABELS ("\\\\Important"))', b'78 (X-GM-LABELS ())', b'79 (X-GM-LABELS ("\\\\Important"))', b'80 (X-GM-LABELS ("\\\\Important"))', b'81 (X-GM-LABELS ("\\\\Important"))', b'82 (X-GM-LABELS ())', b'83 (X-GM-LABELS ())', b'84 (X-GM-LABELS ())', b'85 (X-GM-LABELS ("\\\\Important"))', b'86 (X-GM-LABELS ())', b'87 (X-GM-LABELS ())', b'88 (X-GM-LABELS ())', b'89 (X-GM-LABELS ())', b'90 (X-GM-LABELS ())', b'91 (X-GM-LABELS ())', b'92 (X-GM-LABELS ())', b'93 (X-GM-LABELS ())', b'94 (X-GM-LABELS ())', b'95 (X-GM-LABELS ())', b'96 (X-GM-LABELS ())', b'97 (X-GM-LABELS ("\\\\Important"))', b'98 (X-GM-LABELS ())', b'99 (X-GM-LABELS ())', b'100 (X-GM-LABELS ("\\\\Important"))'])
所以b' 50应该是:
b'50 (X-GM-LABELS (\\\\Inbox))
但仅仅是:
b'50 (X-GM-LABELS ())'
答案 0 :(得分:1)
在我的测试中,始终从X-GM-LABELS
结果中排除当前选定的邮箱。当您致电conn.select()
implicitly selected 'INBOX'时,请从标签列表中排除“收件箱”。
如果您选择了其他邮箱,则会在结果中看到“\\\\收件箱”:
conn.select("[Gmail]/All Mail")
conn.fetch('1:100', '(X-GM-LABELS)')
结果:
('OK', ['21 (X-GM-LABELS ("\\\\Inbox"))'])