simple script to read and parse mailbox

C

Chuck Amadi

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@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@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 []
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top