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 8258689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:37:50+00:00 2026-06-08T02:37:50+00:00

I have a bunch of messages in a gmail mailbox with slightly mangled headers.

  • 0

I have a bunch of messages in a gmail mailbox with slightly mangled headers. I’d like to process and update them automatically using python and gmail’s imap interface. I’d like to download the messages, modify the headers locally, then delete it on the server, then add the fixed message back. The problem is that while the message does seem to be properly deleted, after adding it back the old, bad headers are still present. Complicating matters, if I manually delete the message in gmail and then add the message with the same command in python, the new, good headers appear as desired.

>>> import imaplib
>>> import email
>>> mail = imaplib.IMAP4_SSL('imap.gmail.com')
>>> mail.login('user@gmail.com', 'password')
>>> mail.select('label')
('OK', ['2'])
>>> mail.search(None, 'from', 'bad_string')
('OK', ['2'])
>>> ret,data = mail.fetch('2', '(RFC822)')
>>> msg = email.message_from_string(data1[0][1])
>>> msg['from']
'"Doe, John" <john.doe@bad_string.com>'
>>> new = msg['from'].replace('bad_string', 'good_string')
>>> msg.replace_header('From', new)
>>> msg['from']
'"Doe, John" <john.doe@good_string.com>'
>>> mail.store('2', '+FLAGS', '\\Deleted')
('OK', ['2 (FLAGS (\\Seen \\Deleted))'])
>>> mail.expunge()
('OK', ['2'])
>>> mail.search(None, 'from', 'bad_string')
('OK', [''])
>>> mail.select('label')
('OK', ['1'])

At this point, it seems like python sees the email as deleted. Checking in gmail’s web interface seems to show it as gone, too. There is only one email in the label instead of two at the beginning and the search returns empty.

>>> mail.append('label', None, '"20-Jul-2012 22:30:00 -0400"', str(msg))
('OK', ['[APPENDUID 24 13] (Success)'])
>>> mail.search(None, 'from', 'bad_string')
('OK', ['2'])
>>> mail.search(None, 'from', 'good_string')
('OK', [''])

But the message is back with it’s original bad string. However, if instead of programmatically marking it as deleted and expunging, I delete and empty the trash in gmail’s web interface and then append (still in the same python session as above so running this right after the above output)…

>>> mail.append('label', None, '"20-Jul-2012 22:30:00 -0400"', str(msg))
('OK', ['[APPENDUID 24 14] (Success)'])
>>> mail.search(None, 'from', 'bad_string')
('OK', [''])
>>> mail.search(None, 'from', 'good_string')
('OK', ['2'])

The IMAP settings in gmail are as follows:

  • When I mark a message in IMAP as deleted: Auto-Expunge off – Wait for the client to update the server.
  • When a message is marked as deleted and expunged from the last visible IMAP folder: Immediately delete the message forever
  • 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-06-08T02:37:53+00:00Added an answer on June 8, 2026 at 2:37 am

    So of course the answer uncloaks shortly after posting the question… The significant detail is the line “When a message is marked as deleted and expunged from the last visible IMAP folder“. By default, All Mail is selected as a visible IMAP folder and so the message will always be visible in at least one folder. I believe there are two solutions:

    1. Unselect All Mail as a visible IMAP folder. Then the “When a message is marked…” option behaves as I would have initially expected it to when deleting and expunging from a label. If “archive the message” is selected, it simply removes the label and it is still visible in All Mail. If “move the message to Trash” is selected, the message is moved to the trash label. If “permanently delete forever” is selected, the message is completely deleted.

    2. With all the default folders selected as visible IMAP folders and the default IMAP settings, instead of performing any operations on the message in it’s current label(s) you can instead copy the message to the “[Gmail]/Trash” label, then select the trash label and delete/expunge everything in there. This permanently deletes the message.

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

Sidebar

Related Questions

In Gmail, I have a bunch of labeled messages. I'd like to use an
I have a bunch of messages in encrypted and decrypted form using Blowfish with
In my application at some point I have a bunch of messages scheduled using
I have bunch of strings, some of which are fairly long, like so: movie.titles
I have bunch of lists like this: out[3] [[3]] [1] forum=28&mid=11883 [2] forum=29&mid=11884 out[4]
I have a bunch of code in a routine that looks a bit like
I have a bunch of buttons that have a tapGestureRecognizer linked to them, and
I have a bunch of messages in one of the queues from an old
I have a bunch of messages that are in older schema version. I want
I have a bunch of static Strings that I'd like to store in a

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.