Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > simple script to read and parse mailbox

Reply
Thread Tools

simple script to read and parse mailbox

 
 
Chuck Amadi
Guest
Posts: n/a
 
      06-07-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@sevenofnineythonScript>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@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 []


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My simple script parse output screen and to a new file! chuck amadi Python 11 06-16-2004 10:10 PM
simple script to read and output Mailbox body to file. Chuck Amadi Python 16 06-09-2004 02:19 PM
simple script to read and parse mailbox chuck amadi Python 6 06-07-2004 04:15 PM
simple script to read and parse mailbox Chuck Amadi Python 0 06-07-2004 03:00 PM
Simple Python script to read and output MailBox body to a file Chuck Amadi Python 4 06-06-2004 06:41 PM



Advertisments