simple script to read and parse mailbox

Chuck Amadi chuck at smtl.co.uk
Mon Jun 7 16:54:06 CEST 2004


Hi fishboy I have tried this snippet the output only generates an empty list 
[].
Have goto to add - msg = email.message_from_file(fp) thus print msg

The print output is

chuck at sevenofnine:pythonScript>From nobody Mon Jun  7 15:44:29 2004

How do I get all the body messages I have tried print bodies but just get an 
empty list []. I exspect because that's all what's there So using this script 
can I use the /home/testwwws/Mail/work # what I do when I scan+ to check the 
contents of the mail folder.

I need the body contents that resides in /home/testwwws/Mail/work Mail Folder.

Here's the script. Cheers 


import email
import mailbox
fp = open("/var/spool/mail/chucka")
mbox = mailbox.UnixMailbox(fp, email.message_from_file)
bodies = []
for msg in mbox:
	body = msg.get_payload()
	bodies.append(body)

###########################################################

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 body - not defined
print bodies # produces an emtpy list []





More information about the Python-list mailing list