I'm trying to select a specific mail in my imap folder without having a clear identifier. The only thing I have is a plaintext copy of the mail saved as an eml file.
I tried to load the mail from the file using the email
module but I keep getting errors whenever I run the search function of the imaplib
module starting from the data contained in the mail.
In this instance I'm assuming that the mail comes from the current mailbox.
This is the code I'm using now
import imaplib, email
m = imaplib.IMAP4_SSL(IMAP_SERVER)
m.login(IMAP_USER, IMAP_PWD)
m.select()
mail_file = open('./file.eml')
raw_mail = mail_file.read()
mail = email.message_from_string(raw_mail)
resp, items = m.search(None, '(FROM "' + mail["FROM"] +'")')
But I'm getting the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\user\AppData\Local\conda\conda\envs\py353\lib\imaplib.py", line 707, in search
typ, dat = self._simple_command(name, *criteria)
File "C:\Users\user\AppData\Local\conda\conda\envs\py353\lib\imaplib.py", line 1180, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Users\user\AppData\Local\conda\conda\envs\py353\lib\imaplib.py", line 1011, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SEARCH command error: BAD [b'Error in IMAP command SEARCH: Unknown argument DATE (0.000 + 0.000 secs).']`
How should I do this and what filters can I use to have a reasonable chance of getting exactly the mail I want in the results?
Edit: as requested my mail['FROM']
returns 'test sender\n\t<sender@domain.com>'
I made a test hardcoding a similar format and removing the special characters but of course I'm returned a different mail with matched sender. Also I'm trying to get to the correct mail without altering any data in the most automatic way possible.