pythonimapimap-tools

Imap_tools Multiple NOT Conditions


I am using imap_tools to access and process our business emails. I would like to filter out multiple different from_ messages.

Here's working code for filtering out one from_ address. This returns all emails within the date range except those from notifications@stripe.com

from imap_tools import MailBox, A, AND, OR, NOT
import datetime as dt
from bs4 import BeautifulSoup

username = "info@mysite.co.uk"
password = "mypasword"
imap_server = "mysite.co.uk"

# Get date, subject and body len of all emails from INBOX folder
with MailBox(imap_server).login(username, password) as mailbox:
    for msg in mailbox.fetch(
        AND(NOT(from_="notifications@stripe.com"), date=dt.date(2022, 9, 9))
    ):
        # print(msg.date, msg.subject, msg.html)

        if msg.html:
            soup = BeautifulSoup(msg.html, "html.parser")
            print(msg.from_)
            print(msg.subject)
            print(msg.date)
            print(soup.prettify)
            print(180 * "=")

This is what I have tried:

# Get date, subject and body len of all emails from INBOX folder
with MailBox(imap_server).login(username, password) as mailbox:
    for msg in mailbox.fetch(
        AND(
            NOT(from_=["notifications@stripe.com", "noreply@wise.com"]),
            date=dt.date(2022, 9, 9),
        )
    ):

As per the docs one should pass in a list of str. from_: str | List[str] | None = None

But when I try and pass in a list of email addresses that I don't want to fetch, it just returns all the emails within the date range.

Any ideas? Thanks in advance!


Solution

  • Seems it is limitation of your server.

    I have checked on YANDEX - the same.

    On another server it works.