0

Sorry if this isn't a great question,

Im using imap_tools (https://pypi.org/project/imap-tools/) to fetch mails that are to = emails provided in a list (forwarded to one imap).

At the moment im using an if after fetching mails to see if 'to[0]' is in list, then continuing on. But this checks all emails and feels slower than it needs to be. Is there a way to only fetch the mails based on a to[0] str list in the first place?

Vladimir
  • 6,162
  • 2
  • 32
  • 36
J MGrice
  • 3
  • 3
  • 1
    Yes, you can perform an IMAP search, but this may be very slow depending on the server, especially if you do it over and over. – Max Apr 11 '22 at 14:18
  • How do i pass a list of emails in the fetch of imap tools? The program works quite well at the moment, but it reads all emails to see who its 'to', so obviously its reading all emails before deciding, id like to limit this where i can pass it a list of 20 emails, and then it checks for those, then fetch all that are in the list, to then perform my function on them. I basically want to pass a list of 20 emails, imap tools will iterate through the inbox emails every 60 seconds in async, and add/update a dict of the Key:email address, and the Value:code that my function extracts. – J MGrice Apr 11 '22 at 14:46
  • 1
    How big is the email box? Is there a reason you can’t cache the To:s locally? There’s no need to ever fetch a header more than once from the server; the headers cannot change for any email you already have, as messages are immutable. – Max Apr 11 '22 at 14:48
  • Not that big, its only going to be there for verification codes, but now and again the emails might stack up (I dont wish to delete them until im more confident in the program as I may wish to grab them again and do a function for failures). It may also occasionaly get junk advert mail. Which i will filter out by subject. How do i go about only fetching once and then only fetching new ones? (again, from the predefined list that can be altered). I imagine i can fetch them locally, would i write them to a bundled text file or something? I owe you a drink when im done Max, Thanks as always – J MGrice Apr 11 '22 at 16:28
  • So, yes, you can keep a map of UIDs to To addresses; you could store them in a database or a text file or json or whatever; or just grab them once per run. Then, whenever you need to find a new one, search your saved Tos! If you want to look into server side search, you’ll have to find imap_tools documentation for it. I’ve not used that library myself specifically. – Max Apr 11 '22 at 17:27

1 Answers1

0

I think speed problem not in parsing.

You may search on server and/or use fetch bulk/headers_only args

mailbox.fetch(OR(from_=['Ёлкин', 'fir_man@']), charset='utf-8', bulk=1)
Vladimir
  • 6,162
  • 2
  • 32
  • 36
  • Thanks, this helped, Im very new to python and taken a bigger project for the first time, the issue was my logic I was getting all headers that matched my (subject and from)values, then looking through them to see what To was in my dict. And storing the info if it was When what i should have been doing was nesting in a loop for all items in my dict so I fetch mail that matches my params IF 'To' matched dict[item][email]. Process is much quicker now, I just have to work out how to keep my dict updated without the whole process updating every item in dict every cycle. – J MGrice Apr 12 '22 at 11:51