Fix broken IMAP attachments in Mail.app

I have recently started getting broken images in Mail.app. Often, when someone sent me relatively big pictures (over 1 MB in size), the image ended up being corrupted, with a smaller file size that one would expect. This results in a picture that’s visible up to a certain point, and then rest is usually filled with solid grey by the viewer.

While this could happen because the attached image is indeed broken — that is, if the sender is attaching an image that’s already corrupted on his/her machine —, it’s unlikely that the file gets mangled during the transit. To begin with, the base64 encoding adds lots of CRC (1/3 of the file size, and this alone explains why it’s a very bad idea to send big files through e-mail). In addition to that, no MTA will accept a malformed message: MIME parts must be completed with an extra boundary marker, and the infamous dot on its own line has to be there to commit the DATA part. Using a webmail system makes it even more difficult to corrupt a file in transit, because there is the extra uploading step that ensures that the attachment reaches the server before the message is constructed and finally sent.

If none of this makes sense and you want to know more, please refer to RFC 2821, but the point is that it’s virtually impossible for several people to send corrupted images. It’s way more likely that the problem lies within the recipient’s client.

With OS X’s Mail.app, these problems happen when there is some communication failure during the download of the message. Essentially, if the download gets interrupted for any reason, Mail.app may be unable to notice and will be perfectly happy with the broken file. There are two ways to fix this:

  • The hard way: go to ~/Library/Mail, and for each INBOX you may have, delete anything inside the Attachment folder
  • The easy way: select your Inbox folder within Mail.app and simply run Mailbox → Rebuild

That should take care of the problem. Note that the Rebuild procedure will redownload every message from the server again with all the attachments, essentially resynchronizeing the local cache with whatever is on the server, fixing any discrepancies. It may take some time, but you can keep an eye on the progress by opening the Activity window (Window → Activity, or ⌘0.)