简体   繁体   English

使用 Lambda 中的 imap_tools 在正文中按字符串从垃圾文件夹中获取 email

[英]Fetch email from Junk folder by string in body using imap_tools in Lambda

Using AWS Lambda with a Python script and imap_tools library https://github.com/ikvk/imap_tools#search-criteria使用 AWS Lambda 与 Python 脚本和 imap_tools 库https://github.com/ikvk/imap_tools#search-criteria

I want to search the Junk folder for emails containing a specific string on the body, then move those emails to the Inbox.我想在垃圾文件夹中搜索正文中包含特定字符串的电子邮件,然后将这些电子邮件移至收件箱。 I have the following code which works except it seems to move random emails from the Junk folder and not the ones containing the string in the body.我有以下代码有效,除了它似乎从垃圾文件夹中移动随机电子邮件,而不是在正文中包含字符串的那些。

Does anyone know what I am doing wrong?有谁知道我做错了什么? I'm a bit of a noob to Python so maybe I'm making a rookie mistake.我对 Python 有点陌生,所以也许我犯了一个菜鸟错误。

from imap_tools import MailBox, A, AND, OR, NOT

def lambda_handler(event, context):
    try:
        with MailBox('imap-mail.outlook.com').login('address@msn.com', 'PASSWORD') as mailbox:
            mailbox.folder.set('JUNK')
            mailbox.fetch(AND(body='TESTSTRING'))
            for msg in mailbox.fetch():
                mailbox.move(msg.uid, 'INBOX')
    except:
        print('No emails with tag!')
    finally:
        MailBox.logout
  • read about work fetch and move methods阅读有关工作获取和移动方法的信息
  • try to understand why are you call fetch twice试着理解你为什么要调用 fetch 两次
  • move after loop循环后移动
  • read about context managers, and python at all阅读上下文管理器和 python
  • what line MailBox.logout doing MailBox.logout 在做什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM