0

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.

stovfl
  • 14,998
  • 7
  • 24
  • 51
Forna
  • 149
  • 3
  • 13
  • Edited as requested by @stovfl – Forna Jan 11 '19 at 15:10
  • *`'test sender\n\t'`*: This does not explain the `Error ... SEARCH: Unknown argument DATE ...`. I would prefer to use it without the trailing `\n\t`. Left the Question: **Where does `Unknown argument DATE` come from? – stovfl Jan 11 '19 at 15:50
  • Possible duplicate of [IMAP search for email address in the from field](https://stackoverflow.com/questions/5877529/imap-search-for-email-address-in-the-from-field) – stovfl Jan 11 '19 at 16:05
  • @stovfl I have literally no clue about that, except for the login variables which I left out the code is exactly as written. – Forna Jan 14 '19 at 08:02
  • *"the code is exactly as written."*: This ist different, . **have you tried it like in the dup Answer?** – stovfl Jan 14 '19 at 10:35

0 Answers0