0

I want to search on an IMAP server for whether I have sent, cc, or received emails from certain email addresses, if so retrieve the title and snippets of message bodies.

https://tewarid.github.io/2011/05/10/access-imap-server-from-the-command-line-using-openssl.html

I see the above example using openssl to access imap. But it is not clear whether it is an appropriate tool to search IMAP?

How can I achieve this goal?

1 Answers1

0

It's not an appropriate tool.

Openssl is not an IMAP client. It is able to encapsulate plain text communication in a TLS/SSL tunnel - exactly the kind of encryption around IMAP that happens in IMAPs. So, that article is basically an article that describes how to manually type in IMAP commands using telnet, just that you replace telnet with a cryptographically secure alternative. You're not a command typing machine. Leave that up to a library that knows how to speak IMAP correctly.

You want an IMAP client. CURL could actually download message parts, so, do what you try to do here. So does fetchmail. There's actually quite a few IMAP clients out there - probably also for the scripting language of your choice! I'd strongly recommend you don't use your shell but a properer scripting language, as you really don't want to re-establish a connection to the server for every single email. As a matter of fact, my mail servers would block you after the first couple dozen connection requests in less than a minute.

However, what you do makes little sense in achieving what you want. You should instead ask the server to search on your behalf on the server itself, not try to download all message headers and look for matches in them. IMAP clients tend to offer access to the search protocol that IMAP offers. So go that route.

  • Thanks for your feedback. I need to frequently search emails during a period of time. Are you saying it is better to maintain a connection to the imap server, then submit search commands in that connection, so that I avoid establishing multiple connections for multiple searches? How to achieve this? This seems to take significantly more effect to maintain a connection than connecting at every search. Here is one example I find that connect on each search. https://stackoverflow.com/questions/19001266/how-to-search-specific-e-mail-using-python-imaplib-imap4-search – user1424739 Mar 02 '23 at 14:12
  • What I'm saying is "use a scripting language. For your scripting language, there's a library that does IMAP, correctly. Use that library to search, and download the search results. Do not try to do this by hand." – Marcus Müller Mar 02 '23 at 14:17