Mailing List Archive

controling a Python daemon with mail
Hey all...

I'm interested in writing a Python script that can be controlled by
email. I've read all the relevant Library documentation and the FAQ
and searched the howto's but have not found some specific bits of
information I need. That's when I turn to comp.lang.python. ;-)

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.)

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.

Many thanks,

Preston Landers
Austin, Texas
controling a Python daemon with mail [ In reply to ]
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@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>
controling a Python daemon with mail [ In reply to ]
"Steven D. Majewski" <sdm7g@Virginia.EDU> writes:

> 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.

Thank you! Your message was most helpful. Unlike a snotty reply I
recieved in email that just said "You didn't read the docs."

> 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

Yeah, I considered this. I actually use fetchmail & procmail for my
day to day email. It does have advantages, particularly like you
describe ... the script isn't running continuously but is invoked for
each message.

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

Right, right. I'd want some kind of validation on who's allowed to
send messages to this script.

> 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.

I only glanced briefly at that package but I may go back and look at
it again more carefully. At the time, it seemed like overkill. In
fact, since my needs aren't very complicated, and I already know a bit
about procmail, I may just go with that.

Once again, your message was quite helpful and pointed me in the right
direction. Thanks a lot!!!!

--Preston