Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6544599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:29:07+00:00 2026-05-25T11:29:07+00:00

Using Python and imaplib I am connecting to 2 imap servers (gmail) and trying

  • 0

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.

I am having 2 problems.

  • The date returned by parsing the header is not directly usable in searching, the format is wrong.
  • It is not clear to me which imap search to use to search by date and time as returned by parsing the header.

I have Oldmail and NewMail imap connections.

NewMail.select("[Gmail]/All Mail", readonly=True)
OldMail.select("[Gmail]/All Mail")

Just choosing a random email “6001”

typ, msg = NewMail.fetch('6001', '(BODY[HEADER])')
parser = HeaderParser()
pmsg = parser.parsestr(msg[0][1])

>>> pmsg['Date']
'Tue, 28 Dec 2010 21:56:00 -0700'

But this is not in the right formate to use like, surly there is an easy way 🙂

searchfor = '(ON "' + pmsg['Date'] + '")'
>>> searchfor
'(ON "Tue, 28 Dec 2010 21:56:00 -0700")'
OldMail.search(None, searchfor)

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  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']

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:29:07+00:00Added an answer on May 25, 2026 at 11:29 am

    It looks like the problem is just date formatting. In which case this:

    a = datetime.strptime(pmsg['Date'],"%a, %d %b %Y %H:%M:%S %z")
    

    will give you a date time object and this will give you the message:

    searchfor = '(ON "%s")' %(a.strftime("%d-%b-%Y")
    

    Bear in mind this will give all messages from that date, since it ignores time and timezone info.

    As an alternative, it’s worth looking at your emails and seeing if the “Message-Id:” header could be of help, since it should be the same on each account for genuinely duplicate emails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using imaplib to work with imap in python, however it looks like
import serial import imaplib from time import sleep IMAP_SERVER='imap.gmail.com' IMAP_PORT=993 ser= serial.Serial ('/dev/ttyACM0',9600) M
import serial import imaplib from time import sleep IMAP_SERVER='imap.gmail.com' IMAP_PORT=993 ser= serial.Serial ('/dev/ttyACM0',9600) while
I have a python script using imaplib that connects to a gmail account and
Using Python, I'm storing a date & time as datetime.datetime into GAE. Is there
how do i read mails from my mail box using python?? import getpass, imaplib
I'm having a problem using imaplib on python 2.6 with the latest django svn.
I'm trying to build a C++ extension for python using swig. I've followed the
import imaplib import pprint IMAP_SERVER='imap.gmail.com' IMAP_PORT=993 M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT) rc, resp = M.login('user@gmail.com',
Using Python Regex, I was wondering how to match the patterns (Exercises...) and (Chapter...)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.