[Python-Dev] Split MIME headers into multiple lines near a space

Noam Raphael noamraph at gmail.com
Sun May 29 02:23:59 CEST 2005


I recently used Python to automatically send messages to my gmail
account. I was surprised to find out that some of the words in the
subjects of messages were split by a space character which came from

It turns out that the international (Hebrew) subject was split into
multiple lines by the email package, sometimes in the middle of words.
Gmail treats these line breaks as spaces, so words gets cut into two.
I've checked, and there are email clients which ignore the line
breaks, so the subject looks ok.

I added four lines to the _binsplit function of email.Header, so that
if there is a space character in the string, it will be splitted
there. This fixes the problem, and subjects look fine again. These
four lines (plus a comment which I wrote) are:

    # Try to find a place in splittable[:i] which is near a space,
    # and split there, so that clients which interpret the line break
    # as a separator won't insert a space in the middle of a word.
    if splittable[i:i+1] != ' ':
        spacepos = splittable.rfind(' ', 0, i)
        if spacepos != -1:
            i = spacepos + 1

These lines should be added before the last three lines of _binsplit.

Do you think it's ok? Could this be added to email.Header?

(Should I send this as a patch? It's just that the patch list was full
of IDLE patches, and this change is really small, so I thought that it
would be easier to post it here. Please tell me if I was wrong.)

Thank you,
Noam Raphael

More information about the Python-Dev mailing list