simple script to read and output Mailbox body to file.

Chuck Amadi chuck at smtl.co.uk
Mon Jun 7 18:20:42 CEST 2004


Sorry to bovver you again (again) here's script.

I still can't see why the get_payload() doesn't produce 
the plain text message body of an emails in the testwwws users mailbox.
As you can see I have tried a few things but no joy what am I missing.

Is the another snippet in relation to get_payload to access the body contents 
print and process to a file.

Cheers

Chuck

ds9:[pythonScriptMail] % 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 paraemeter
# mode can be 'r' when the file will only be read, 'w' for only writing
#(an existing file with the same name will be erased), and 'a' opens the file
# for appending; any data written to the file is automatically added to the 
end.
# 'r+' opens the file for both reading and writing. The mode.
output =("/tmp/SurveyResults", "w+a")
#output =('/tmp/SurveyResults','w')
 
# open() returns a file object, and is most commonly used with two arguments:
# "open(filename, mode)".
# /home/testwwws/Mail/work
#
# fp The file or file-like object passed at instantiation time. This can be 
# used to read the message content.
fp = open("/var/spool/mail/testwwws")
 
#fp = open("/home/testwwws/Mail/work")
 
# 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 = []
 
msg = email.message_from_file(fp)
# 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)
# output.close() to close it and free up any system resources taken up by the 
open file.
# After calling output.close(), attempts to use the file object will 
automatically fail.
#print bodies
print fp
print msg
print msg['body']
# print body - NameError: name 'msg' is not defined
#
#print >> output,bodies
#output.close()
#print the bodies list  of the messages.
print bodies





More information about the Python-list mailing list