【问题标题】:Get emails with imap_tools with python使用 python 使用 imap_tools 获取电子邮件
【发布时间】:2022-09-27 13:09:55
【问题描述】:

我想接收来自特定发件人的所有今天的电子邮件文本,并使用 imap-tools 将它们放入 Outlook 的列表中

我做了以下功能,但问题是它不会从上午 12:00 到下午 12:00 检索电子邮件,有没有办法指定获取正确消息的时间?

def get_emails(username, password, sender):
    from imap_tools import MailBox, A

    emails = []

    with MailBox(\'outlook.office365.com\').login(username, password, \'INBOX\') as mailbox:
        for msg in mailbox.fetch(
                A(
                    A(date_gte=datetime.date.today()),  # get the today\'s emails
                    A(from_=sender),         # from the specific senderEmailAddress
                ),
                mark_seen = True
            ):
            if msg.subject == \"Subject\":
                emails.append(msg.text)
        return emails

    标签: python imap-tools


    【解决方案1】:

    使用date (imap_tools documentation):

    from imap_tools import MailBox, A
    
    def get_emails(username, password, sender):
        emails = []
    
        with MailBox('outlook.office365.com').login(login_name, login_password, 'INBOX') as mailbox:
            for msg in mailbox.fetch(
                    A(date=datetime.date.today(), from_=sender),
                    mark_seen = True
                ):
                if msg.subject == "Subject":
                    emails.append(msg.text)
            return email
    

    【讨论】:

    • 这完全一样的工作....
    • @David 这将检索今天的所有电子邮件;不是来自未来。这保证了时间将是您指定的时间,因为它是今天。
    • 出于某种原因,它对我不起作用,我还注意到,当我尝试打印每封电子邮件的接收日期时,例如:msg.date 我得到错误的日期
    • @David 你能给我们datetime.date.today()datetime.datetime.utcnow().astimezone().tzinfo 的输出吗?
    • >> datetime.datetime.utcnow().astimezone().tzinfo datetime.timezone(datetime.timedelta(seconds=10800), 'GTB Daylight Time') >> datetime.date.today(): datetime.date(2022, 9、25)
    【解决方案2】:

    IMAP 中没有按时间过滤,只有按日期过滤。

    在客户端按时间过滤。

    【讨论】:

      猜你喜欢
      • 2022-09-30
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 2013-04-30
      • 2012-04-15
      • 2021-10-10
      • 2021-10-07
      相关资源
      最近更新 更多