【问题标题】:Check whether pdf attachement does'nt exsist in gmail python using imap使用 imap 检查 gmail python 中是否不存在 pdf 附件
【发布时间】:2021-04-21 09:56:40
【问题描述】:

我可以根据参数搜索邮件,如果存在附件,请执行相关逻辑, 但是,如果搜索成功并且附件不存在,我将无法跟踪它。 任何建议,将不胜感激。 代码如下,

    if part.get_content_maintype() == 'multipart':
       continue
    if (part.get('Content-Disposition')) is None:
      continue
    filename = part.get_filename()
    if bool(filename):
       print('have attachment')
       # call relevant function
       # This works

    else:  
      print('no attachment')
     #call relevant function
     # This doesn't work

【问题讨论】:

  • 你真的希望有人能理解缺乏细节的问题吗?

标签: python gmail imap


【解决方案1】:

https://github.com/ikvk/imap_tools

from imap_tools import MailBox, A

with MailBox('imap.mail.com').login('test@mail.com', 'pwd', 'INBOX') as mailbox:
    for msg in mailbox.fetch(A(all=True)):
        print(msg.date, msg.subject)
        if any(att.filename.lower().endswith('.pdf') for att in msg.attachments):
            print('- has pdf')
        else:
            print('- no pdf')
        

*我是库作者

不要这么懒,用搜索

【讨论】:

    猜你喜欢
    • 2019-01-09
    • 1970-01-01
    • 2012-04-13
    • 2012-04-05
    • 2015-04-06
    • 2020-08-24
    • 2012-08-09
    • 2012-10-10
    • 1970-01-01
    相关资源
    最近更新 更多