From: sdm7g at Virginia.EDU (Steven D. Majewski)
Date: Fri, 23 Apr 1999 21:01:54 -0400 (EDT)
Subject: controling a Python daemon with mail
In-Reply-To: <84k8v3t4sw.fsf@mail.utexas.edu>
References: <84k8v3t4sw.fsf@mail.utexas.edu>
Message-ID: <Pine.A32.3.90.990423203148.23052I-100000@elvis.med.Virginia.EDU>
Content-Length: 3133
X-UID: 436                                                  

On 23 Apr 1999, Preston Landers wrote:

 
> I've looked at the mailbox.UnixMailbox package.  I am able to write a
> script that retrieves each message in /var/spool/mail/myuser and
> displays the sender, recipient, and subject.  I am able to generate a
> reply using /usr/bin/mail.  But for the life of me, I cannot look at
> the body of the incoming message.  Silly me.  Where in the
> documentation is this describe?  10 points to anyone who can point out 
> the specific place to me.  


Yeah -- mailbox and rfc822 aren't the prettiest modules in the Python
libraries! Here's where the class browser in the Mac Python IDE comes
in handy, along with some interactive snooping. 


>>> mb = mailbox.UnixMailbox( open( 'OneGig:Python+11', 'r' ))
>>> mb
<mailbox.UnixMailbox instance at 2735700>

## mb is a mailbox 

>>> dir(mb)
['fp', 'seekp']
>>> a = mb.next()
>>> a
<rfc822.Message instance at 2749760>

## a is a message.

>>> dir(a)
['dict', 'fp', 'headers', 'seekable', 'startofbody', 'startofheaders', 
'status', 'unixfrom']
 >>> a
<rfc822.Message instance at 2749760>
>>> a.fp
<mailbox._Subfile instance at 27496a0>
>>> a.fp.fp
<open file 'OneGig:Python+11', mode 'r' at 27353c0>

# a's _Subfile is what you want, speficially, for the message body: 

>>> print a.fp.read() 



> Periodically, I want my daemon to check a standard Unix mail spool
> file for new messages.  Under certain conditions, I want the script to 
> act on those messages (ie, do its thang on the contents of the message 
> and mail the results back to the sender.)
 

However, the typical way to do this sort of thing on unix is to 
redirect all  messages with a .forward file to dispatcher program
that gets each message one at a time on it's standard input. 
Thus you don't have to check the spool periodically -- the program
gets triggered as part of the mail delivery. There are unix programs
like procmail & filter that already do this. ( and procmail has 
a companion program, formail, which among other things can split
a mailbox up into separate messages, pipeing each one to a new
procmail process, thus simulating the delivery process in batch
mode. ) 

You can use a python script in place of procmail -- in which case,
it doesn't have to split a mailbox into separate messages -- it gets
a single message on it's standard input. 

Or, you can use procmail to selectively send particular messages to
another script. 

Or, you can select a subset of messages and redirect them into a 
different file, which can be the input for your batch processing. 


You might also want to look a Mailman  -- which is a mailing list
manager written in Python. It may already have most of the tools 
you need for your responder. 

 

---|  Steven D. Majewski   (804-982-0831)  <sdm7g at Virginia.EDU>  |---
---|  Department of Molecular Physiology and Biological Physics  |---
---|  University of Virginia             Health Sciences Center  |---
---|  P.O. Box 10011            Charlottesville, VA  22906-0011  |---

    Caldera Open Linux: "Powerful and easy to use!" -- Microsoft(*)
     (*) <http://www.pathfinder.com/fortune/1999/03/01/mic.html>
 





