Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > simple script to read and output Mailbox body to file.

Reply
Thread Tools

simple script to read and output Mailbox body to file.

 
 
Chuck Amadi
Guest
Posts: n/a
 
      06-08-2004
Hi using the breakdown script I managed this minis mail.read() just print mail.

mbox = mailbox.UnixMailbox(fp)
for mail in mbox:
print mail
break #just look at one message

Here's the output of the script. Cheers chuck

chuck@sevenofnine:~/pythonScript> python testmail.py
Return-Path: <(E-Mail Removed)>
Received: from enterprise.smtl.co.uk (enterprise.smtl.co.uk [193.131.77.1***])
by ds9.smtl.co.uk (8.12.10/8.11.0) with ESMTP id i4PDJXgN006468
for <(E-Mail Removed)>; Tue, 25 May 2004 14:19:35 +0100
Message-Id: <(E-Mail Removed)>
Subject: WWW Survey
To: http://www.velocityreviews.com/forums/(E-Mail Removed)
From: (E-Mail Removed)
Date: Tue, 25 May 2004 14:19:14 +0100
X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on ds9.smtl.co.uk
X-Spam-Status: No, hits=0.3 required=5.0 tests=NO_REAL_NAME autolearn=no
version=2.63
X-Spam-Level:

chuck@sevenofnine:~/pythonScript>




 
Reply With Quote
 
 
 
 
Chuck Amadi
Guest
Posts: n/a
 
      06-08-2004
This now works ! as you stated below.Now all the body messages is this down
using bodies [] list.

Cheers Chuck

mbox = mailbox.UnixMailbox(fp)
for mail in mbox:
msg = email.message_from_file(mail)
print `mail` #backticks
print mail['Subject']
print mailget_content_type()
break #just look at one message

Gives me this:

chuck@sevenofnine:~/pythonScript> python getSurveyMail.py
mail
WWW Survey
text/plain
<wwwsurvey>
Country you are currently working in:<country>UNITED STATES</country>
Postal code or ZIP Code of your facility if you know it:<zipcode>
12345</zipcode>
Your Profession:<profession>Nurse</profession>
How long you have been qualifed:<qualified>&lt; 1 Year</qualified>
Is Wound Management Your primary role:<iswoundmgt>A major part of your daily
activities</iswoundmgt>
Specific area of interest in the list below:<specificareas>Veterinary
medicine</specificareas>
Do you read a paper &ndash; based wm journal:<journal>Yes</journal>
If Yes your comments:<jcomments>bugs news</jcomments>
Favorite topics in the World Wide Wounds:<favorite>['Dressings information',
'News']</favorite>
Further Information: required:<furtherinfo>all covered really</furtherinfo>
Your Email Address:<email>(E-Mail Removed)</email>
</wwwsurvey>


that works, then I done this

msg.get_payload() should return the body. How about all the body messages in
file.

Thanks


 
Reply With Quote
 
 
 
 
Chuck Amadi
Guest
Posts: n/a
 
      06-08-2004
Hi again after breaking down the script into easier chunks .
I now get this .

Here's the output from the script but WHAT about all messages bodies

Cheers

chuck@sevenofnine:~/pythonScript> python getSurveyMail.py
mail
WWW Survey
text/plain
<wwwsurvey>
Country you are currently working in:<country>UNITED STATES</country>
Postal code or ZIP Code of your facility if you know it:<zipcode>
12345</zipcode>
Your Profession:<profession>Nurse</profession>
How long you have been qualifed:<qualified>&lt; 1 Year</qualified>
Is Wound Management Your primary role:<iswoundmgt>A major part of your daily
activities</iswoundmgt>
Specific area of interest in the list below:<specificareas>Veterinary
medicine</specificareas>
Do you read a paper &ndash; based wm journal:<journal>Yes</journal>
If Yes your comments:<jcomments>bugs news</jcomments>
Favorite topics in the World Wide Wounds:<favorite>['Dressings information',
'News']</favorite>
Further Information: required:<furtherinfo>all covered really</furtherinfo>
Your Email Address:<email>(E-Mail Removed)</email>
</wwwsurvey>


 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-09-2004

Hi After createing a mailbox using packf
packf +/home/chuck/testwwws all -file testbox and thus cp 3 5 6 (emails) .

I get output with this except only one email header and body message , the
commented out stuff , does that iterate or can it produce all current 3 email
body messages. Cheers Im nearly there.

for mail in mbox:
# body = mail.get_payload()
# bodies.append(body)
# msg = mail.message_from_file(mail)
print 'mail'
print mail['Subject']
print mail.get_content_type()#text/plain
print mail.get_payload()
break # just look at one message



 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-09-2004
Hi all forget my last post about all email body messages I added a comment
before break . Sorry for reading my own comments lines.

I get output desired .Now to pass to an external File.

for mail in mbox:
# body = mail.get_payload()
# bodies.append(body)
# msg = mail.message_from_file(mail)
print 'mail'
print mail['Subject']
print mail.get_content_type()#text/plain
print mail.get_payload()
# break # just look at one message



 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-09-2004
Hi again

I get output desired .Now to parse the fileobject and all body messages to an
external File named SurveyResults.txt
Is this the correct way to go about it .

for mail in mbox:
# body = mail.get_payload()
# bodies.append(body)
# msg = mail.message_from_file(mail)
print 'mail'
print mail['Subject']
print mail.get_content_type()#text/plain
print mail.get_payload()
# break # just look at one message

# The File SurveyResults.txt must all ready exist.
output = open("SurveyResults.txt", 'w') # mode 'w' means open the file for
writ-ing (any data already in the file will be erased)
mailout = mail.get_payload()
output. writelines(mailout)

output. close() # this is at end of the script

Here's my error but I thought mail is my file

Traceback (most recent call last):
File "getSurveyMailRev2.py", line 37, in ?
mailout = mail.get_payload()
NameError: name 'mail' is not defined




 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-09-2004

Hi please could someone point to what I have done wrong as the script runs
with no errors but the file I am trying to parse the print statement output
too instead of just the screen/command line to another file.

I have digested the Input and Output and text formatting docs and I read
something about using stdout to a file . Can someone point me in the right
direction.

Cheers my code below.

################################################## #############
## 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: 09/06/04
## Revision 1. make copy to continue called getSurveyMailRev2.py
################################################## #############

#The following line makes it run itself(executable script on UN*X)
#!/usr/bin/env python

import sys
import os
import email
import mailbox
import StringIO

# 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.

# The File SurveyResults.txt must all ready exist.

#mailout = open("/home/chuck/pythonScript/SurveyResults.txt") # mode 'w' means open the file for writ-ing
# (any data already in the file will be erased)
#output.writelines(mailout)
mailout = file("/home/chuck/pythonScript/SurveyResults.txt")

# open() returns a file object, and is most commonly used with two arguments:
# "open(filename, mode)".
fp = open("/home/chuck/pythonScript/testbox")

# 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 = []

# mail is the file object
for mail in mbox:
print 'mail'
print mail['Subject']
print mail.get_content_type()#text/plain
print mail.get_payload()


# First open the testbox file to read(r) and write(w)to the SurveyResults.txt
#fp = open("testbox","r")
mailout = open("/home/chuck/pythonScript/SurveyResults.txt","w")

# Read the testbox file into a list then copy to
# new file.
for mail in fp.readlines():
mailout.write(mail)

print "testbox file copied...to SurveyResults.txt"

# Now close the files
fp.close()
mailout.close()

#mailout.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.





 
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
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 script to read and parse mailbox Chuck Amadi Python 0 06-07-2004 02:54 PM
Simple Python script to read and output MailBox body to a file Chuck Amadi Python 4 06-06-2004 06:41 PM
Not detecting body.scrollTop and body.scrollLeft in IE6 London Boy Javascript 2 01-12-2004 08:44 AM



Advertisments