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: 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/

相关文章:

python - 动态输出CSV文件

Python:numpy 数组列表,不能做 index()?

python - 线上的标记

python - 在 Qscintilla/PyQt4 中使用 SCI_SEARCHINTARGET

Python邮件: encoded attachments are truncated

python - 套接字错误 "[Errno 9] Bad file descriptor"可能是什么原因

python - 在 Ubuntu 上使用 Python 3.2 的 rsvg

python - Django REST FrameWork JWT 不允许提供数据或自行解码

python - 计数器似乎没有正确添加

python 在 Debug模式下在 c++ 中工作,但在 exe 文件中不工作