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

  • 本文关键字:特定人 python 获取 python
  • 更新时间 :
  • 英文 :


所以我在网上找到了一些代码,但我没有找到我所寻求的:这是我改进的,但我仍然无法从标题中提到的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: jusernamenSecond 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.comnSecond 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)

最新更新