title: How can I search a mail in my imap folder starting from an EML file with python?
body: <p>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.</p>
  
  <p>I tried to load the mail from the file using the <code>email</code> module but I keep getting errors whenever I run the search function of the <code>imaplib</code> module starting from the data contained in the mail.</p>
  
  <p>In this instance I'm assuming that the mail comes from the current mailbox.</p>
  
  <p>This is the code I'm using now</p>
  
  <pre><code>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"] +'")')
  </code></pre>
  
  <p>But I'm getting the following error:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):  </p>
  </blockquote>
  
  <pre><code>  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    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).']`
  </code></pre>
  
  <p>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?</p>
  
  <p>Edit: as requested my <code>mail['FROM']</code> returns <code>'test sender\n\t&lt;sender@domain.com&gt;'</code></p>
  
  <p>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.</p>