simple script to read and parse mailbox

Chuck Amadi chuck at smtl.co.uk
Mon Jun 7 06:14:28 EDT 2004


Hi all exspecailly fishboy here's the script I'm just waiting to get 
confirmation where im going to run the script form.

I have added a output =('/tmp/SurveyResults','w+a') which I believe will 
process the body messages data to this file for future work ie database 
loading.

Also that using I can add 'a' opens the file # for appending any data written 
to the file is automatically added to the end.Is this logical .I have tried to 
# comments to aid my learning process So bear with me.


chuck at sevenofnine:~/pythonScript> cat getSurveyMail.py
###############################################################
## This script will open and parse email messages body content.
## This Python script will reside on Mail Server on ds9:
## Emails are all plain/text you could just write the following
## Which will leave a list of strings , each one a message body.
## The Survey User is testwws and the .procmailrc file folder is
## Survey . i.e /home/testwws/Mail/inbox/Survey .
###############################################################
## file:getSurveyMail.py Created : 06/06/04 Amended date: 07/06/04
###############################################################
 
#The following line makes it run itself(executable script on UN*X)
#!/usr/bin/env python
 
import sys
import os
import email
import mailbox
 
# Open the testwws user mailbox (tmp user chuck)
# fp denotes factory paremeter
 
output =('/tmp/SurveyResults','w+a')
fp = open("/var/spool/mail/chuck")
 
#fp = open("/var/spool/mail/testwws")
 
# message_from_file returns a message object struct tree from an
# open file object.
 
mbox = mailbox.UnixMailbox(fp, email.message_from_file)
# list of body messages.
bodies = []
 
# for loop iterates through the msg in the mbox(mailbox).
# Subparts of messages can be accessed via the -
# get_payload() method will return a string object.
# If it is multipart, use the "walk" method to iterate through each part and 
the
# get the payload.In our case it's not multipart So ignore.
# for part in msg.walk():
#       msg = part.get_payload()
#       # do something(print)
 
for msg in mbox:
        body = msg.get_payload()
        bodies.append(body)
# Print to screen for testing purposes.
# print the bodies list  of the messages.
print bodies
chuck at sevenofnine:~/pythonScript> vi getSurveyMail.py
chuck at sevenofnine:~/pythonScript> python getSurveyMail.py
[]

The last line I assume would list all the body messages within the bodies list []/

Cheers for all your help list.





More information about the Python-list mailing list