1

I'm playing around with SMTP and using email.mime to provide the header structure. For some reason when a try to add a header that exceeds a certain length a line break is added into my header line.

e.g.

from email.mime.text import MIMEText
message = 'some message'
msg = MIMEText(message)
msg.add_header('some header', 'just wondering why this sentence is continually cut in half for a reason I can not find')

print msg['some header']
print msg

print msg['some header'] prints:-

some header: just wondering just wondering why this sentence is continually cut in half for a reason I can not find

print msg prints:-

some header: just wondering why this sentence is continually cut in half for a
 reason I can not find

One thing I did discover is that the length at which it's cut off is a combination of the header title and its value. So when I shorted 'some header' to 'some', the line return changes to after 'reason' instead of before.

It's not just my viewing page width :), it actually sends the email with the new line character in the email header.

Any thoughts?

user788462
  • 1,085
  • 2
  • 15
  • 24

1 Answers1

5

This is correct behaviour, and it's the email package that does this (as well as most of the email generating code out there.) RFC822 messages (and all successors to that standard) have a way of continuing headers so they don't have to be a single line. It's considered good practice to fold headers like that, and the tab character that indents the rest of the header's body means the header is continued.

Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
  • See also: http://www.ietf.org/rfc/rfc2822.txt Section 2.1.1 describes the recommended line length of 78 characters, and Section 2.2.3 describes line "folding". – Marty Feb 13 '12 at 21:04
  • @thomas Thanks for the quick reply. Is there a way to override the email package so it doesn't force a new line? – user788462 Feb 13 '12 at 21:08
  • The only real way, I believe, would be to instantiate `email.header.Header` directly, or replace `email.header.MAXLINELEN` with a larger value. I suggest not doing this, though. What the `email` package is generated is entirely compliant and the encouraged format. Perhaps you should not be doing the thing that requires that messages are represented in equivalent but different formats. – Thomas Wouters Feb 14 '12 at 09:00