CRLF handling in the rfc822 module

N

Nikolaus Schulz

Hi,

while playing with the rfc822 module and imaplib, I've found the
following weird behaviour. Quickstart: rfc822.Message(fp) instantiates
a rfc822.Message object from a file object fp. The docs for the rfc822
module say here:

Input lines as read from the file may either be terminated by CR-LF or
by a single linefeed; a terminating CR-LF is replaced by a single
linefeed before the line is stored.

I've tested that with a message read from an IMAP server and a
locally generated message; since I'm running Linux, the local one has LF
line terminators, while the IMAP message uses CRLF.

The following code snippet demonstrates that these two cases yield
different line terminators in the resulting rfc822.Message object.

#v+
#!/usr/bin/python

import imaplib
import rfc822

# Assignments omitted
# imap_server =
# imap_username =
# imap_password =
# imap_folder =

def print_header(msg_str):
import cStringIO
msg = rfc822.Message(cStringIO.StringIO(msg_str))
print repr(msg.headers[1])
print repr(msg.getrawheader('Envelope-to'))
print repr(msg.getheader('Envelope-to'))

imap_srv = imaplib.IMAP4_SSL(imap_server)
imap_srv.login(imap_username, imap_password)
imap_srv.select(imap_folder)
result, response = imap_srv.fetch(1, '(RFC822)')
imap_srv.close()
imap_srv.logout()
print_header(response[0][1])

local_msg = """\
Return-path: (e-mail address removed)
Envelope-to: nikolaus@localhost
Delivery-date: Fri, 12 May 2006 05:37:22 +0200
Message-ID: <[email protected]>
Date: Thu, 11 May 2006 20:30:44 -0700
From: Grargravarr <[email protected]>
To: Nikolaus Schulz <[email protected]>
Subject: Bogus CRLF handling in Python
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Dummy mail body.
"""
print_header(local_msg)
# EO: demo
#v-

Running the demo script gives this:

#v+
'Envelope-to: nikolaus@localhost\r\n'
' nikolaus@localhost\r\n'
'nikolaus@localhost'
'Envelope-to: nikolaus@localhost\n'
' nikolaus@localhost\n'
'nikolaus@localhost'
#v-

Isn't that inconsistent? Have I hit a bug?
Please enlighten me!

Have a nice day,
Nikolaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[bug] imaplib case sensitive 0
IMAP Tkinter Issue 0
IMAP mailwatcher w/Tkinter 0
imaplib -- can't read body 0
IMAP Problems 0
Memory errors with imaplib 6
IMAP Checking Folder Size 1
IMAP Folder Size Information 0

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top