gpt4 book ai didi

python - 从特定标签的 Gmail 中获取所有电子邮件

转载 作者:太空宇宙 更新时间:2023-11-03 21:48:22 25 4
gpt4 key购买 nike

我想从 Gmail 中获取名为 important 的特定标签的所有电子邮件。我正在使用 imaplibpython 2

下面是我的代码,

import email, getpass, imaplib, os

detach_dir = '.'
user = raw_input("Enter your GMail username:")
pwd = getpass.getpass("Enter your password: ")



# connecting to the gmail imap server
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user,pwd)

m.select("important")

resp, items = m.search(None, "ALL")

items = items[0].split()

print len(items)

for emailid in items:
resp, data = m.fetch(emailid, "(RFC822)")
email_body = data[0][1]
mail = email.message_from_string(email_body)


if mail.get_content_maintype() != 'multipart':
continue

print "["+mail["From"]+"] :" + mail["Subject"]


for part in mail.walk():

if part.get_content_maintype() == 'multipart':
continue


if part.get('Content-Disposition') is None:
continue



filename = mail["From"] + "_hw1answer"

att_path = os.path.join(detach_dir, filename)


if not os.path.isfile(att_path) :

fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()

显示错误,

imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED

但是,如果我使用 INBOX 那么它就可以工作。

工作时m.select("inbox")

推荐的实现方式是什么?

最佳答案

m.select("重要") 失败。

如果您想要特殊的加星标文件夹,它可能被命名为“[Gmail]/Important”。使用list()命令查找服务器使用的名称。

关于python - 从特定标签的 Gmail 中获取所有电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52304678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com