[Tutor] File I/O, rfc822, and stdin/stdout

Sheila King sheila@thinkspot.net
Sat, 13 Jan 2001 22:35:27 -0800


I'm getting really confused and frustrated here.

Ultimately, my goal is to write some scripts to handle my incoming e-mail. My
web host uses qmail as an MTA and with .qmail files I can invoke scripts to
process my mail. The scripts receive the incoming e-mail from stdin and if
they write output to stdout, that output can be delivered as the resulting
e-mail message.

Anyhow, I've started with some much simpler stuff. Some of it works, some of
it doesn't.

I'm wanting to work with the rfc822 module, for it's capabilities in handling
headers.

Here is one of my scripts:
----------------------------------------
#! /usr/bin/python

import rfc822

raw_message=open("message3.txt","r")

inheaders=rfc822.Message(raw_message)
body=raw_message.read()

print inheaders
print
print body
----------------------------------------

I run this in a DOS window on my home machine. The file "message3.txt"
contains an e-mail message (in mbox format). It seems to work fine. It reads
the message headers and the message body in separately, from a file, and then
prints them to stdout.

If I could just get them to read the message from stdin, changing the source
of the input, then this should at least pipe a message through the script on
my web host, without changing or in anyway sorting, filtering or redirecting
it.

Well, I can't seem to achieve that.

I did have one script, that worked on my web host, that I invoked from a
.qmail file:

-----------------------------------------
#! /usr/bin/python

import sys

infile = sys.stdin
text = infile.read()
print text
print "\nThis tag appended"
print
print "Python rules."
-----------------------------------------

This one worked fine. It read the incoming mail from stdin, and wrote it back
out to stdout, adding the  lines "This tag appended" and "Python rules" to the
end of the e-mail, and it was then delivered correctly.

Of course, the problem with the above script, is it doesn't give me any easy
way of handling the message headers, for filtering purposes.

So, I now tried to combine them, into a script that would be of more use to
me:
----------------------------------------
#! /usr/bin/python

import sys, rfc822

raw_message = sys.stdin

inheaders=rfc822.Message(raw_message)
body=raw_message.read()

print inheaders
print
print body
----------------------------------------

Supposedly, it should read from stdin, retrieve the headers and body
separately, and then write them to stdout. But I'm getting no e-mail
delivered.

So, then I thought I'd try to just read an e-mail, and print it to a file.

I used this script:
---------------------------------------
#! /usr/bin/python

import sys, os
input = sys.stdin
messagetext=input.read()
outfile = open("outmessage.txt", "w")
os.chmod("outmessage.txt", 0664)
sys.stdout=outfile
print messagetext
outfile.close()
---------------------------------------

I have to change the permissions in the script, because the mail script runs
with a different UserID, and then I can't read the resulting file.

Anyhow, the above script does appear to be called. The output file is created,
and the permissions are correctly set. But the file is empty.

So, I guess I'm not really reading stdin correctly, but I'm at a loss to
figure out what's wrong. I've been looking through the tutorial and the module
docs a lot today. I've even been searching on dejanews for some posts on these
topics in comp.lang.python. But I haven't been able to figure this problem
out.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/