Martin H
Martin H

Reputation: 155

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 [email protected]

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

username = "[email protected]"
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_="[email protected]"), 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_=["[email protected]", "[email protected]"]),
            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!

Upvotes: 0

Views: 379

Answers (1)

Vladimir
Vladimir

Reputation: 6726

Seems it is limitation of your server.

I have checked on YANDEX - the same.

On another server it works.

Upvotes: 1

Related Questions