title: Search imap using parsed date from another email
body: <p>Using Python and imaplib
  I am connecting to 2 imap servers (gmail) and trying to match emails between them using the date their date time.</p>
  
  <p>I am having 2 problems.</p>
  
  <ul>
  <li>The date returned by parsing the header is not directly usable in searching, the format is wrong.</li>
  <li>It is not clear to me which imap search to use to search by date and time as returned by parsing the header.</li>
  </ul>
  
  <p>I have Oldmail and NewMail imap connections.</p>
  
  <pre><code>NewMail.select("[Gmail]/All Mail", readonly=True)
  OldMail.select("[Gmail]/All Mail")
  </code></pre>
  
  <p>Just choosing a random email "6001"</p>
  
  <pre><code>typ, msg = NewMail.fetch('6001', '(BODY[HEADER])')
  parser = HeaderParser()
  pmsg = parser.parsestr(msg[0][1])
  
  &gt;&gt;&gt; pmsg['Date']
  'Tue, 28 Dec 2010 21:56:00 -0700'
  </code></pre>
  
  <p>But this is not in the right formate to use like, surly there is an easy way :-)</p>
  
  <pre><code>searchfor = '(ON "' + pmsg['Date'] + '")'
  &gt;&gt;&gt; searchfor
  '(ON "Tue, 28 Dec 2010 21:56:00 -0700")'
  OldMail.search(None, searchfor)
  
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 620, in search
      typ, dat = self._simple_command(name, *criteria)
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 1060, in _simple_command
      return self._command_complete(name, self._command(name, *args))
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 895, in _command_complete
      raise self.error('%s command error: %s %s' % (name, typ, data))
  error: SEARCH command error: BAD ['Could not parse command']
  </code></pre>
  
  <p>My goal is to find matching emails between the 2 servers after failed transfer using googles migration app. Then delete the email on the old server if they have transferred. If you can suggest a better way of doing this that would be helpful also.</p>