473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

CRLF handling in the rfc822 module

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: ga********@gargravarr.org
Envelope-to: nikolaus@localhost
Delivery-date: Fri, 12 May 2006 05:37:22 +0200
Message-ID: <12***@bogus.org>
Date: Thu, 11 May 2006 20:30:44 -0700
From: Grargravarr <ga********@gargravarr.org>
To: Nikolaus Schulz <sp*****@spam.bin>
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
May 21 '06 #1
0 1081

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael B Allen | last post by:
How can I zoom in on what's going on below? $ archivemail -n ~/Maildir/.Lists.* /home/miallen/Maildir/.Lists.CIFS: I would have archived 11 of 29 message(s) in 0.1 seconds...
1
by: John Doe | last post by:
I've never used a factory and would like to get some help. The class mailbox.UnixMailbox has a defination for a factory. __init__(self, fp, factory=<class rfc822.Message>) where fp is a file...
4
by: McKirahan | last post by:
How would I use a regular expression to remove all trailing Carriage Returns and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance. Also, are they any great references for learning...
4
by: Nemesis | last post by:
Hi all, I found that the function parsedate_tz of the rfc822 module has a bug (or at least I think so). I found a usenet article (message-id: <2714d.q75200@myg.winews.net>) that has this Date...
1
by: praba kar | last post by:
Dear All, I am new to python world. I have pasted my code which I used it to build rfc822 format mails for webbased mailing system task(which is like to yahoo.com web interface). Now I am...
3
by: Phoe6 | last post by:
Hi all, Have a look at this snippet, I have a file direct.txt and I want to read it as rfc8222.Message() so that I get the Subject: and Mood: as Dict Keys and content separately, but I am unable...
10
by: Bob Quintal | last post by:
I'm receiving data in text files from a unix sustem that onlu puts a Lf character at the end of each row. Access insists on Cr or CrLf or it sees the file as 1 row 700Kb wide and chokes. Is...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
5
by: Robert Latest | last post by:
Hello, I'm new to Python but have lots of programming experience in C, C++ and Perl. Browsing through the docs, the email handling modules caught my eye because I'd always wanted to write a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.