Tuesday, March 10, 2015

"Why Is BBEdit Corrupting My Thunderbird or Postbox Mailboxes?"

Instructions for repairing your mailboxes commonly tell you to edit the files in a text editor, and BBEdit is usually recommended for the Mac. Unfortunately, for Thunderbird or Postbox mailboxes, this is about the worst advice you can get. More often than not, merely editing and saving the file in BBEdit will seriously corrupt the mailbox.

Why is that? The problem lies in how both the text editor and the email apps work, and in how those ways interact. Thunderbird and Postbox store received email messages with Unix line endings -- "LF" or "line feed" -- as they should. But when you compose a message, those apps create and store it with Windows line endings -- "CRLF" or "carriage return, line feed." So, unless you keep your sent and received messages in separate mailboxes, you get mixed line endings in the same file. (Postbox was built on Thunderbird code, so the similarities should be no surprise.)

As the folks at BBEdit explained it to me, some text editors are good at handling mixed line endings in files, while others are more interested in helping you "normalize" line endings -- make them consistent. Apple's TextEdit and Notepad++ on Windows are examples of the former. BBEdit is one of the latter.

When BBEdit opens a file, it looks at the beginning to decide what kind of line ending it's using. If that file is a Thunderbird or Postbox mailbox and starts with a received message, then BBEdit decides on Unix line endings. And it expects to find those same line endings throughout the file -- as would be common in a text file.

The problem comes when BBEdit reaches a sent message with its Windows line endings. Because it expects single-character Unix line endings, it interprets the two-character combination of carriage return and line feed as _two_ line endings, and puts two line feeds in their place. In other words, it inserts a blank line between each line of text. And of course, it never warns you.

So, what happens when you next try to view that mailbox's messages in Thunderbird or Postbox? Both of those apps expect to find each message's headers in a set of single-spaced lines at the top. When they instead find double-spaced lines, they don't recognize the headers. All the messages you've sent are still listed, but they appear without subject, sender, or recipient, and with the current date replacing the original! The data is still safe in your mailbox, but Thunderbird and Postbox can't read it.

Is there any way to use BBEdit on a mailbox that doesn't damage it like that? Yes. You can open the file with the Open command on the File menu, unchecking the option to "Translate line breaks." After the file opens -- which can take a long time -- use the Normalize Line Endings command on the Text menu. Then, if you need to, at the bottom of the window, change the file's line endings from "Classic Mac (CR)" or "Windows (CRLF)" to "Unix (LF)."

You can then edit as you like, and Thunderbird or Postbox will have no problem. But this won't stop those apps from inserting more messages with Windows line endings -- so if you need to reedit, you'll likely have to go through the whole procedure again. Really, it's probably simpler to edit the mailbox in TextEdit instead.

What if you've already corrupted the file? If you can't replace from a backup, you'll have to remove the double spacing. To do that, you can reopen the file in BBEdit, find the double-spaced messages, and remove the extra blank lines by replacing each pair of line endings with a single one. (Yes, it's safe to edit the file like that.) The correct header info will then magically reappear in Thunderbird or Postbox. To make the repair quicker and easier, you can probably first use the email app to move all the mailbox's corrupted messages to a new mailbox by themselves.

As a long-term solution, we can hope that the developers of these apps make some changes. BBEdit's developers tell me there are changes coming down the line that may help with mixed line endings. And maybe Thunderbird's or Postbox's developers will wake up and realize there is no place for Windows line endings in a Mac app.

By the way, you'll notice I haven't discussed what happens in BBEdit when it opens a mailbox starting with Windows line endings but with Unix endings mixed in later. I'm afraid you'll have to work that out for yourself!

No comments:

Post a Comment