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.

Advertisements

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

  1. Chris Bloom

    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 (foo.bar.net) 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.

    • devender

      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. Hot Chicken Guy

    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. Pizza Party

    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. GermanG

    @pizza party:

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

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

  5. Bitt Faulk

    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. Spec

    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 “offlineimap.py”, 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 “./cleanbox.py “, and it’ll list the e-mail messages that aren’t RFC822 compliant. If you want to delete them, run, “./cleanbox.py delete”, and it will delete them. I’ve tested this on my own sandbox/exchange server, but as always, use at your own risk.

    cleanbox.py can be found(for now): http://club-ubuntu.pastebin.com/UiANqxd5
    offlineimap.py can be found(for now): http://club-ubuntu.pastebin.com/ST7wQBtg

    I would credit the original offlineimap.py author, however, I have no idea who s/he was. The only thing I added to offlineimap.py is the main() function.

    cleanbox.py 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. Steven Boothe

    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.

  8. Steven Boothe

    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:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s