File I/O, rfc822, and stdin/stdout

Greg Jorgensen gregj at pobox.com
Mon Jan 15 02:27:06 EST 2001


I worked on a similar project, but eventually replaced qmail with
postfix and my own scripts with procmail. Here's what worked for me
before I abandoned it:

----
import cStringIO
import rfc822

s = sys.stdin.read()       # read all input at once
f = cStringIO.StringIO(s)  # string to file-like object
m = rfc822.Message(f,1)    # parse headers
b = f.read()               # get message body
...
print str(m)               # print headers
print
print b                    # print body
---

In my application I needed to keep the entire message text around
(headers + body), so I read it all at once and use cStringIO to pass it
to rfc822.

--
Greg Jorgensen
Portland, Oregon, USA
gregj at pobox.com



In article <72i36t8mmo27mi2nmv5iahr4ospps9crat at 4ax.com>,
  Sheila King <sheila at thinkspot.net> wrote:
> On Sun, 14 Jan 2001 01:40:43 -0600, Eric Rahmig <erahmig at minn.net>
wrote in
> comp.lang.python in article <mailman.979458074.1585.python-
list at python.org>:
>
> :If you're not using qmail's 'preline' utility in your .qmail files
> :that might cause a problem. Without the use of 'preline' the email
> :message your script sees will NOT start with a From line and will
> :NOT contain a Return-Path header.
>
> You know, at some point I had been using preline, but eventually I
removed it.
> It didn't seem to be accomplishing much (of course, I had so many
other errors
> at that point ...
>
> Anyhow, just now I put it back in, to see what would happen. At
least, it
> delivered an e-mail.
>
> :I'm new to Python and have no experience with the rfc822 module. If
> :it is strict in its message parsing and expects a message to contain
> :a From line and a Return-Path header then use of preline should help
> :you:
> :
> :.qmail-foo:
> :|preline somescript.py ...
>
> Well, apparently rfc822 requires those lines, because at least now it
is
> delivering a message.
>
> Here is the script I just ran:
>
> -------------------------------------------
> #! /usr/bin/python
>
> import sys, rfc822
>
> raw_message = sys.stdin
>
> inheaders=rfc822.Message(raw_message)
> body=raw_message.read()
>
> print inheaders
> print
> print body
> -------------------------------------------
>
> Here is the e-mail I just received:
> -------------------------------------------
> Return-Path: <sheila at thinkspot.net>
> Delivered-To: xthinkspot-myuserID at thinkspot.net
> Received: (qmail 7982 invoked by uid 41121); 14 Jan 2001 15:30:48 -
0000
> Date: 14 Jan 2001 15:30:48 -0000
> Message-ID: <20010114153048.7981.qmail at nine.futurequest.net>
> From: sheila at thinkspot.net
> Cc: recipient list not shown: ;
> Delivered-To: xthinkspot-mytestaddress at thinkspot.net
>
> <rfc822.Message instance at 80bf6d8>
> -------------------------------------------
>
> That's it. The whole message.
>
> first of all, the fact that there is no To: field or Subject field in
the
> message, shows that the headers that came out of my script are
somehow broken
> (because I did have a To: and a Subject: in the test mail I sent).
Secondly,
> why is "print body" causing this line?
> <rfc822.Message instance at 80bf6d8>
> ?
>
> When I ran the following script on a DOS prompt, where I have changed
only the
> source of input:
> -------------------------------------------
> #! /usr/bin/python
>
> import sys, rfc822
>
> #raw_message = sys.stdin
> raw_message=open("message4.txt","r")
>
> inheaders=rfc822.Message(raw_message)
> body=raw_message.read()
>
> print inheaders
> print
> print body
> -------------------------------------------
>
> The entire message body (along with headers) is printed out to the
screen.
>
> Anyhow, I guess I will go back to trying to write the messages to
files, so I
> can see exactly what the script is putting out, and try to figure out
what the
> problem is.
>
> Thanks for suggesting preline. It did help.
>
> --
> Sheila King
> http://www.thinkspot.net/sheila/
> http://www.k12groups.org/
>



Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list