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

  • SEARCH
  • Home
  • 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 4604646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:19:29+00:00 2026-05-22T00:19:29+00:00

From a different account, I sent myself an email with the subject Test de

  • 0

From a different account, I sent myself an email with the subject Test de réception en local. Now using IMAP, I want to find that email searching by subject.

When doing a search for ALL and finding the email among the output, I see:
Subject: =?ISO-8859-1?Q?Test_de_r=E9ception_en_local?=

So now, searching with imap, I try:

M = imaplib.IMAP4_SSL('imap.gmail.com', 993)
M.login('user@gmail.com', 'password')
M.select('[Gmail]/All Mail')

subject = Header(email_model.subject, 'iso-8859-1').encode() #email_model.subject is in unicode, utf-8 encoded
typ, data = M.search('iso-8859-1', '(SUBJECT "%s")' % subject)
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][1])
M.close()
M.logout()

print 'Fin'

If you print out subject, you see that the result appears just the same as what I’m getting from the IMAP server on my prior, more-broad search. Yet, it doesn’t seem to make a match when doing this more specific search.

For the search, I have tried everything I can think of:

typ, data = M.search('iso-8859-1', '(HEADER subject "%s")' % subject)
typ, data = M.search('iso-8859-1', 'ALL (SUBJECT "%s")' % subject)

And others that I can’t recall at the moment, all without any luck.

I can search (and match) for emails that have subjects that only use ASCII, but it doesn’t work with any subject that has an encoding applied. So…

With IMAP, what is the proper way to search for an email using a subject that has an encoding applied?

Thanks

  • 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-22T00:19:30+00:00Added an answer on May 22, 2026 at 12:19 am

    When talking to IMAP servers, check with IMAP RFC.

    You must remove extra quotes, and you must not encode the strings. Also, charset specifies the charset of the search query, not the charset of the message header. This should work (works for me):

    M.search("utf-8", "(SUBJECT %s)" % u"réception".encode("utf-8"))
    # this also works:
    M.search("iso8859-1", "(SUBJECT %s)" % u"réception".encode("iso8859-1"))
    

    Edit:

    Apparently some servers (at least gmail as of August 2013) support utf-8 strings only when sent as literals. Python imaplib has a very limited literal arguments support, the best one can do is something like:

    term = u"réception".encode("utf-8")
    M.literal = term
    M.search("utf-8", "SUBJECT")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application deals with e-mails coming from different sources, e.g. Outlook and IMAP mailboxes.
I want to gather data from different data servers located in Europe and Asia.
When I try to execute a view that includes tables from different schemas an
I'm testing several user accounts and don't want to setup different emails to test
I want to retrieve the recently viewed pictures from IE. I know that all
I'm using soapUI 3.6.1 and I want to test a SOAP-interface with a test
I have a template string and an array of parameters that come from different
I have an inbound email handler that's working. However, right now I'm worried about
I would like to get data from from different webpages such as addresses of
I have many emails coming in from different sources. they all have attachments, many

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.