Fixing Mail import crashes on Leopard

After restoring from a Time Machine backup, Apple Mail would crash every time I tried to re-import my email archive. The problem seems to be affecting a lot of people who have mail that pre-dates OS X. The same crash also happens after migrating to a new machine.

Quick fix

Copy the following command and paste it into your Terminal. Press return and wait a few minutes. Once the command finishes, Mail should be able to import your messages.

grep -lZr 'Content-disposition: attachment' ~/Library/Mail/Mailboxes/ | xargs -0 ruby -i -pe 'gsub(/(Content-type:[^;]*;\s*name=)"(.*)"/){$1+(if !$2.nil? then $2.dump.gsub(/\\\\/, "\\") end)}'

The problem

The problem is specific to older, MacRoman encoded email messages with attachments whose filename includes non-ascii characters. For whatever reason, that freakshow edge-case combination will crash Apple Mail every time it tries to import those files. If by some stroke of luck you already have these messages in your email archive (I did), they can still crash Mail when trying to rebuild the containing mailbox.

The solution is sort of simple, just rename the attachment in the .emlx file. The harder part is finding which file to edit.

We’ll use grep to recursively search for a common token in these older files, then pass the filenames to a Ruby snippet which will filter the line containing the bad characters. Here is a section of a suspect message’s header:

Content-type: application/octet-stream; name="SPWH 4.01ü.sit";
Content-disposition: attachment
Content-transfer-encoding: base64

The first bolded line contains the attachment name which is causing the problem, who knows what that umlaut was originally. The second bolded line is what we’re telling grep to locate, I couldn’t get anything to dependably match the oddball characters. “Content-disposition” appears to be an older attachment syntax and didn’t appear in any of my messages from after 2001.

While the Ruby script could be run from find’s exec command, it wouldn’t be particularly efficient. Calling the script from find would pass every .emlx file in the Mailboxes directory through Ruby’s gsub, which is almost wholly unnecessary (and much slower). Only 10 of my 57,007 messages needed fixing, 99.98% of them were fine.

Is this safe?

Since the most common way to encounter this bug involves Time Machine restores or migration to a new machine, most users should already be backed up. If something should go wrong, just restore the backup’s Library/Mail folder over the messed up one. You can also copy or zip the Library/Mail folder to another drive to be even safer.

That said, I ran dozens of iterations of this solution against copies of my personal Mail archive without issue. And besides, if you’re reading this, you might not have any of your old mail, so how much worse could it really get?

If you’re still worried, copy your ~/Library/Mail/Mailboxes folder to another drive, run the script, then compare directories with something like Apple’s FileMerge. That will show you exactly which files have changed and what was changed inside them.

Renaming the attachment should be perfectly safe since the full encoded file contents are stored in the message. All the attachment name does is specify the filename, in a sense, it’s totally arbitrary.

I first submitted this bug with Apple back in May, if you have a developer account with Apple, please file a dupe for radar: 5912997

9 Responses to “Fixing Mail import crashes on Leopard” Comments Feed for Fixing Mail import crashes on Leopard

  • Thank you very much for this!

    I was struggling to find a solution for this exact problem earlier today as I was trying to grep my 14k emlx files to locate the bad ones after a failed import…

    I was aware of this problem because of a client who, until very recently, was using Entourage on Mac OS 9. My would crash upon receiving one of her emails, and I was forced to retrieve and purge her message from my POP mailbox via webmail.

    OS 9 being what it is, it couldn’t handle the long filename attachments that she received so it truncated them with an ellipsis (…) character somewhere in the middle. That’s likely what the ü character you see above was meant to represent.

    • Hi I’m wondering if this is safe for Snow Leopard? Thanks much!

  • I have tried applying the most recent fix from Apple, with no success.
    The email still crashes.
    This is a temporary fix—I think it works because it shortcuts an erroneous path from the launch bar Mail icon—but only temporarily. ( I have tried removing the launch bar E-mail Icon and replacing it with the icon from the Applications folder; but that did not work either).
    1) Save any Pages document to the desktop
    2) Right click on the document on the desktop
    3) Left Click “Open with”
    4) Scroll to the “Mail” icon and select it with a left click
    5) When the email with the attachment has opened, close it down by clicking on the red close button at the top left.
    6) Your email should now run
    This provides some stability for a while before the next occurrence.
    If anyone can fix this instability please post it here
    David W

  • @david_williams Though this probably isn’t the most productive place to post it, what is causing the crash you’re seeing? You never mentioned specifically what the problem is, so it’s hard to be much help.

  • Thank you!

  • did a copy paste of your script on terminal but import still hangs. still couldnt recover mail and it keeps restarting and trying to import again to no avail.

    any ideas?

  • oh by the way.. just noticed that your script is for leopard. running snow leopard. i also noticed that the path may be wrong but im not sure (might be different for SL).

    • I am runnning snow leaopard and have the same problem.  I don’t really know terminal.  What should the script be in leopard.  Do I have to change anything to be specific to my drive?

  • It is my pleasure to read this page,I look forward to reading more.

Leave a Reply