General, ruby

Deleting those pesky RFC-822 in-compatible emails with Ruby

If you have landed on this post from Google you already know what I am talking about, if not read the below intro.

I work for an “Enterprisy” company, so the standard here is using the exchange server and I am on Linux using thunder bird. Every once in a while I get those emails that the exchange server cannot convert to an RFC-822 compatible format and thunder bird chokes on em and even the web mail cannot handle these, the only solution so far has been going to the web mail and selecting the mail and moving it to trash.

Finally I wrote a script to automate the whole thing, it telnets into the server checks each message to see if it is RFC-822 compatible and if not moves it into Deleted Items, you can find the script here. (It’s on GitHub feel free to fork it)

Usage ruby telnet-imap.rb <server> <username> <password> <dryrun>

All arguments are self explanatory, dry run can be y/n

PS : if you mange you blow up your inbox you are on your own!!

UPDATE (2010/03/12): Looks like this has taken a life of its own, please see the comments to find a better/newer versions in different languages.

14 thoughts on “Deleting those pesky RFC-822 in-compatible emails with Ruby

  1. I’m trying to run this for a co-worker, but keep getting the following DEBUG messages. Is this normal?

    D, [2009-02-24T09:12:03.250000 #2252] DEBUG — : * OK Microsoft Exchange Server 2003 IMAP4rev1 server version 6.5.7638.1 ( ready.
    D, [2009-02-24T09:12:03.250000 #2252] DEBUG — : ? BAD Protocol Error: “Unexpected additional characters at the end of command”.
    D, [2009-02-24T09:12:03.250000 #2252] DEBUG — : examining folder INBOX
    D, [2009-02-24T09:12:03.250000 #2252] DEBUG — : ? BAD SELECT command received in invalid state.

    Also, FYI – the script appears to continue even if the initial login fails.

    1. Nop that is not normal, I could offer 2 suggestions

      1. Try to log in using telnet and see if you can run the commands and if everything works.
      2. Put a debug on the command being sent to make sure it is sending the right command.

      Sorry can’t offer anymore help unless I was running it against your server myself.
      Thanks for trying the script.

  2. Just wanted to drop a Thank You out there. I’ve been frustrated in this same scenario for a few weeks and your script worked like a charm

  3. I’m running the script now on a 5 GB email box with 37K emails. I’m currently in dryrun mode. Is there a way that the script can grab only the headers as opposed to the full email? It’s printing a lot of unnecessary information to the cmd screen. And will likely take many hours to complete.

  4. @pizza party:

    Change the logger output to
    @log =‘log.txt’); @log.level = Logger::DEBUG

    I’m using
    tail -f log.txt | grep INBOX-message
    to watch its progress

  5. It’s pausing 15s (within about 10ms) between each message fetch. Any idea why?

    Also, it would probably make more sense to fetch RFC822.SIZE instead of RFC822.TEXT, since that’s what Thunderbird is doing, and it transfers a heck of a lot less data.

  6. Heya, well, I didn’t like the ruby script for numerous reasons. The first one was it required passwords to be sent via CLI, and it didn’t support IMAPS, and I don’t know ruby. 🙂

    So, I rewrote your script to use python, imaplib, and it uses the gnome-keyring to fetch usernames and passwords. I modified another script I found online that handles the gnome-keyring integration. If you run “”, you’ll get a few prompts, and that’ll store (securely) your server/username/password into the gnome-keyring. (You can verify with seahorse!)

    You only need to do that once. After that, you can run “./ “, and it’ll list the e-mail messages that aren’t RFC822 compliant. If you want to delete them, run, “./ delete”, and it will delete them. I’ve tested this on my own sandbox/exchange server, but as always, use at your own risk. can be found(for now): can be found(for now):

    I would credit the original author, however, I have no idea who s/he was. The only thing I added to is the main() function. is losely based on the original “telnet-imap.rb”.

    PS: If someone wants to take this code and mirror it/distribute it/whatever, please feel free. I’m going to consider this as public domain.

  7. Or you could just:
    -mark all messages read in thunderbird
    -go to OWA
    -view only unread messages
    -delete all of those

    1. Thanks! This is a remedy that even a non-technical person can employ – it worked perfectly for me.

  8. Or, rather than allow the end users to suffer, the dev’s could do a noble deed by writing the code to account for the spurious condition and live with the disgust of having to deal with Microsoft’s poor RFC compliance as Apple chose to do for Apple Mail. So there is that option too, but I could not hold it against anyone for not going that route.

  9. Awesome news:

    Today I received a push update for Mozilla Thunderbird 6.0 and I no longer receive those annoying message boxes anymore. So it appears the beautiful people at Mozilla have resolved this issue. Thus I think we should really take a moment to consider how much we appreciate the Open Source community and those who make it so beautiful a place to live and work.

    Best regards,

    Steven Boothe

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s