0

With IMAPlib I am using:

self.m.select("Inbox") 
typ, mail = self.m.search(None, "(ALL)", f'(SENTSINCE {datesince})')

where:

datesince = (datetime.date.today() - datetime.timedelta(days=self.timerange)).strftime("%d-%b-%Y")

print("Datesince:", datesince)

Gives:

Datesince: 08-Oct-2021

But in my log file I can see that all the following emails were dealt with, some are from previous dates way in the past, why is the since filter not precise?

This is how I loop through the emails:

typ, mail = self.m.search(None, "(ALL)", f'(SENTSINCE {datesince})')
    ids = mail[0]
    id_list = ids.split()
    latest_email_id = int(id_list[-1])
    oldest_email_id = int(id_list[0])
    countmail = latest_email_id - oldest_email_id + 1
    print("countmail:", countmail)

    for self.i in tqdm(range(latest_email_id, oldest_email_id - 1, -1)):
        print("Incrementation:",self.i)
        typ, x = self.m.fetch(str(self.i), '(RFC822)')

I expunge deleted emails only after the loop.

Iterating over all mails to extract NAVs countmail: 16 Incrementation: 265 subject: Amundi CDA Abs RTN MLT-STRGY Fund (ARMS) - 10082021 price delay Date: Fri, 8 Oct 2021 22:10:34 +0000 Mail not deleted Incrementation: 264 subject: TR: Cashoutflow 01110066S03, DeAM-Fonds ENPT CORP Date: Thu, 7 Oct 2021 11:29:46 +0200 Mail not deleted Incrementation: 263 subject: R: ALIFOND: OUTFLOW Date: Fri, 8 Oct 2021 16:21:30 +0200 Mail not deleted Incrementation: 262 subject: RE: ONBOARDING - PF92671 – BOK GLOBAL DEVELOPED MARKETS FIXED INCOM - LAUNCH DATE 08/10/2021 Date: Fri, 8 Oct 2021 16:19:51 +0200 Mail not deleted Incrementation: 261 subject: RE: ONBOARDING - PF92671 – BOK GLOBAL DEVELOPED MARKETS FIXED INCOM - LAUNCH DATE 08/10/2021 Date: Fri, 8 Oct 2021 14:58:31 +0200 Mail not deleted Incrementation: 260 subject: RE: STANLIB IM ADVICE - CreLiq_Amundi-MM Global Bond - 06/10/2021 Date: Fri, 8 Oct 2021 12:50:06 +0200 Mail not deleted Incrementation: 259 subject: RE: September 30,2021 NAV AM-MY EURO CREDIT FUND Not received Date: Fri, 8 Oct 2021 12:01:27 +0200 Mail not deleted Incrementation: 258 subject: RE: Problème perf daido Date: Thu, 7 Oct 2021 17:34:51 +0200 Mail not deleted Incrementation: 257 subject: NAV officielle PF82299 Mirae - intégration PAMS Date: Tue, 12 Jan 2021 11:19:41 +0100 Mail not deleted Incrementation: 256 subject: NAV officielle PF82299 Mirae - intégration PAMS Date: Tue, 12 Jan 2021 11:19:41 +0100 Mail not deleted Incrementation: 255 subject: RE: ONBOARDING - PF90548 – CAVOM OBLIGATIONS INTERNATIONALES - LAUNCH DATE 06/10/2021 Date: Wed, 6 Oct 2021 13:22:53 +0200 Mail not deleted Incrementation: 254 subject: RE: Aged claim !!- LU3503 DVD query ISIN: SE0000115446 ex date 30/06/2021 Account: 373662 -- Claim #186735 --- 1053736620W [AS3PAM-MAS-13894] Date: Thu, 7 Oct 2021 03:04:26 +0000 Mail not deleted Incrementation: 253 subject: FW: Trade FX Settlement Query -LU3510 Monnet -373669 Date: Tue, 28 Sep 2021 13:51:08 +0200 Mail not deleted Incrementation: 252 subject: RE: Devises - Desjardins + Desjardins Pooled Fund State Farm & TPIC Pension Plan September 2021 Date: Wed, 29 Sep 2021 14:23:09 +0200 Mail not deleted Incrementation: 251 subject: ONBOARDING - PF90548 – CAVOM OBLIGATIONS INTERNATIONALES - LAUNCH DATE 06/10/2021 Date: Thu, 30 Sep 2021 13:48:26 +0200 /home/ludo915/automate_PDP_IMAP/attachments/Onboarding Form CAVOM Obligations Internationales.xlsx /home/ludo915/automate_PDP_IMAP/attachments/CAVOM_Obligations_Internationales_2021-09-30_1630583721207.pdf

Ludogiraud
  • 25
  • 7
  • Does this answer your question? [How to use variables with imaplib SINCE and BEFORE](https://stackoverflow.com/questions/63724964/how-to-use-variables-with-imaplib-since-and-before) – Vladimir Oct 11 '21 at 07:28
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Oct 12 '21 at 11:58
  • Yes @Vladimir thank you, although I am getting a few emails way in the past, outside the SINCE boundaries, I believe this is due to maternity leaves and people on holidays turning off their computers, and being part of a given mailing list, coming back to work & switching back on their outlook. – Ludogiraud Oct 12 '21 at 18:40

1 Answers1

0

It is precise. IMAP allows you to search for email both by arrival date at the recipient's server and by the sender's stated date. As you can see from the spec the relevant keys are since and sentsince.

arnt
  • 8,949
  • 5
  • 24
  • 32
  • Why am I retrieving emails with dates prior to 08/10/2021 then? – Ludogiraud Oct 09 '21 at 19:01
  • Maternity Leaves? And people on holidays with unsent mails on their outlook mailboxes? – Ludogiraud Oct 09 '21 at 22:28
  • You probably want the SINCE key, and since IMAP is not timezone aware you may get up to 23 hours or 23 hours less than what you expect. – Max Oct 09 '21 at 23:45
  • @Max ok but I am getting an email from Date: Tue, 12 Jan 2021 11:19:41 +0100 this is almost exactly 9 months ago, is there a logical explanation for this? I will try the SINCE key and solve this post if it works as expected. – Ludogiraud Oct 10 '21 at 01:28
  • Look at the `internaldate` value you can retrieve via IMAP, it's what's used. It is usually a second or so after the last date mentioned in a `Received` header field, but an IMAP client can set it to any date (using the `append` command). – arnt Oct 10 '21 at 20:55