Thunderbird: moving email from local Junk folder to IMAP folder yields "Message contains invalid header"

Whenever I try to move an email from a local Junk folder to an IMAP folder in Thunderbird, I get the following error message:

The current command did not succeed. The mail server responded: Message contains invalid header

If Thunderbird's Junk folder is an IMAP folder on the server, then after Thunderbird has moved messages to that folder, I can successfully move messages from Junk back into to some other IMAP folder. However, if the Junk folder is not on the server, then moving a message from the local Junk folder to an IMAP folder yields the aforementioned error.

The only interesting thing I've found about this error is "Message contains invalid header" from the MozillaZine Knowledge Base. That article officially is about importing folders from another email client, and does not mention the Junk filter as another possible cause. However the proposed solution is not very helpful since it requires manual editing of the message box files.

Any better ideas?

EDIT: make sure you read the comments before answering the question.


Found this article : "Fix invalid mail headers when moving from Thunderbird to IMAP", where a Python script is proposed that fixes all mailbox files in a given directory.

Even if you don't use Python, it should be quite easy to do the same in another language or a text-editor or sed. Apparently, this fixed the problem for the author of the article.