gpt4 book ai didi

python - 在python中获取未读邮件中特定人员的附件

转载 作者:行者123 更新时间:2023-12-04 14:50:23 28 4
gpt4 key购买 nike

所以我在网上找到了一些代码,但我没有找到我要找的东西:这是我改进的地方,但我仍然无法从标题中提到的 2 个条件中获得附件。有人能帮我吗 ?我也使用 Imbox,因为我没有找到更好的东西。谢谢!

import os
from imbox import Imbox # pip install imbox
import traceback
from pathlib import Path

host = "imap.gmail.com"
download_folder = "/home/pi/download/folder"
allowed_email = "email-allowed.txt"
path = '/home/pi/eink_picture_frame'
pwfile = Path(path+"/maillogin.pwd") # define file with username and password for the mail account

if pwfile.is_file():
with open(pwfile,"r") as pf:
pw_lines = [line.rstrip('\n') for line in pf]
username = pw_lines[0]
password = pw_lines[1]
else:
print("Please provide login information in file 'maillogin.pwd'!\nFirst line: jusername\nSecond line: password")
quit()

allowfile = Path(path+"/email-allowed.txt") # define file with username and password for the mail account
if allowfile.is_file():
with open(allowfile,"r") as af:
email_lines = [line.rstrip('\n') for line in af]
mymail = email_lines[0]
emmail = email_lines[1]
else:
print("Create a list of allowed email addresses in 'email-allowed.txt'!\nFirst line: joeshmo@aol.com\nSecond line: user@google.com")
quit()

if not os.path.isdir(download_folder):
os.makedirs(download_folder, exist_ok=True)

mailS = Imbox(host, username=username, password=password, ssl=True, ssl_context=None, starttls=False)
#messages = mailS.messages() # defaults to inbox
URmail = mailS.messages(unread=True)
messages = mailS.messages()
inbox_messages_frommymail = mailS.messages(sent_from=mymail)


for (uid,message) in URmail and inbox_messages_frommymail :
print(message)
mailS.mark_seen(uid)# optional, mark message as read

for (idx, attachment) in enumerate(message.attachments):
try:
att_fn = attachment.get('filename')
download_path = f"{download_folder}/{att_fn}"
print(download_path)
with open(download_path, "wb") as fp:
fp.write(attachment.get('content').read())

except:
print(traceback.print_exc())

mailS.logout()

最佳答案

您需要将两个过滤器合并为一个,它会过滤两个 inbox_messages_frommymail = mailS.messages(sent_from=mymail,unread=True)

关于python - 在python中获取未读邮件中特定人员的附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69168348/

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