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-07-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


 
Reply With Quote
 
 
 
 
William Park
Guest
Posts: n/a
 
      06-07-2004
Chuck Amadi <(E-Mail Removed)> wrote:
> 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.

[snip]

You still haven't answered central questions.
- Do you want email bodies in separate files, or all email bodies in
one file?
- Do you want to collect these email bodies as they come in, or
periodically from a 'mbox' file?

--
William Park, Open Geometry Consulting, <(E-Mail Removed)>
No, I will not fix your computer! I'll reformat your harddisk, though.
 
Reply With Quote
 
 
 
 
fishboy
Guest
Posts: n/a
 
      06-07-2004
On Mon, 07 Jun 2004 17:20:42 +0100, Chuck Amadi <(E-Mail Removed)>
wrote:

>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)
># # do something(print)
>
>for msg in mbox:
> body = msg.get_payload()
> bodies.append(body)


Trimmed out a few comments

Ok, you dont need to pass UnixMailbox email.message_from_file if your
going to explictly parse later with msg = email.message_from_file(fp)

Your parsing the email message twice.

If there are still problems, try breaking it down a bit

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

'mail' is a file obj pointing at a single email, so print mail.read()
should display one email, with headers. If that doesn't work we have
problems before we get anywhere

If that gives us something sensible for an output, try parsing the
email.

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

Should give us something like:

<email.Message.Message instance at 0xa0e442c>
Re: Best IDE?
text/plain

And if that works, then

msg.get_payload() should return the body.

><{{{*>

 
Reply With Quote
 
chuck amadi
Guest
Posts: n/a
 
      06-08-2004
William Park wrote:

>Chuck Amadi <(E-Mail Removed)> wrote:
>
>
>>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.
>>
>>

>[snip]
>
>You still haven't answered central questions.
> - Do you want email bodies in separate files, or all email bodies in
> one file?
> - Do you want to collect these email bodies as they come in, or
> periodically from a 'mbox' file?
>
>
>

Hi There ,

I would like all email bodies in one file.Collect these email bodies as they come in, or periodically from a 'mbox' file? using cron.

I would lie cron to run the script and empty the mail user box in question to a file for extraction to a database.

Cheers

Chuck





 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-08-2004
I have tried breaking it down a bit as per your post.
when I run it I still dont see any results even to my output file .
Im logged in as myself ansd trying to access the mailbox on the mailserver
which is mounted via nfs.The mail server has only version Python 1.5 So doesnt
know about the email module.

Cheers

output =('/tmp/SurveyResults','w')

fp = open("/var/spool/mail/chuck")


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



 
Reply With Quote
 
fishboy
Guest
Posts: n/a
 
      06-08-2004
On Tue, 08 Jun 2004 09:49:41 +0100, Chuck Amadi <(E-Mail Removed)>
wrote:

>I have tried breaking it down a bit as per your post.
>when I run it I still dont see any results even to my output file .
>Im logged in as myself ansd trying to access the mailbox on the mailserver
>which is mounted via nfs.The mail server has only version Python 1.5 So doesnt
>know about the email module.
>
>Cheers
>
>output =('/tmp/SurveyResults','w')
>
>fp = open("/var/spool/mail/chuck")
>
>
>mbox = mailbox.UnixMailbox(fp)
>for mail in mbox:
> print mail.read()
> break #just look at one message
>
>


What does a
$less /var/spool/mail/chuck
give you?

UnixMailbox is expecting a single file with all the emails
concatenated in it. UnixMailbox just splits them out one at a time.

Hrmmm, looking in the source, it recommends that you use
PortableUnixMailbox instead. If you can see the emails with the
'less' command, maybe that will work.

If you don't see any emails with 'less', I'm left wondering where your
emails are at?

><{{{*>


 
Reply With Quote
 
William Park
Guest
Posts: n/a
 
      06-08-2004
chuck amadi <(E-Mail Removed)> wrote:
> William Park wrote:
> >You still haven't answered central questions.
> > - Do you want email bodies in separate files, or all email bodies in
> > one file?
> > - Do you want to collect these email bodies as they come in, or
> > periodically from a 'mbox' file?
> >

> Hi There ,
>
> I would like all email bodies in one file.Collect these email bodies
> as they come in, or periodically from a 'mbox' file? using cron.
>
> I would lie cron to run the script and empty the mail user box in
> question to a file for extraction to a database.


Since your running Linux, you can simply do manual or periodic parsing
of mbox file and put the email bodies into 'file',
formail -I '' -s < mbox >> file
Or, use Procmail recipe as email comes in,
:0 b:
* some_condition
| cat >> file

Ref:
man formail procmail procmailrc procmailex

Homework:
Translate it to Python.

--
William Park, Open Geometry Consulting, <(E-Mail Removed)>
No, I will not fix your computer! I'll reformat your harddisk, though.
 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-08-2004

Hi again I using this to test as instructed it runs on my local machine with
my /var/spool/mail/chuck dir
thats mounted on the mail server using nfs .
Thus I still get no output only when I print mbox.

NB Im digesting formail doc's and man pages.

Cheers

Chuck

#!/usr/bin/env python


import email
import mailbox


fp =open("/var/spool/mail/chuck")
mbox = mailbox.UnixMailbox(fp)


for mail in mbox:
print mail.read()
break # Just reads one message



 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-08-2004
Nice One I was thinking I was going mad at home this works .
But $ less /var/spool/mail/chuck produced no output.

I use scan+ to check and go thru users email folder . i.e

cd /home/User/Mail/Inbox <Folder in Question> say spam Folder

> scan +Lottery > show 1 example below:


chuck@sevenofnine:~/pythonScript> cd /home/chuck/Mail
chuck@sevenofnine:~/Mail> ls
:0 Chuck drafts Inbox mhnjVMtAk pete post2ic2cT post57nXqr
postG9QD2D postW78Qle Root spam
Beth chucksMail from mbox Operator Pete post2iybnf post8ReyCU
posthArGZR postz5FN1w rootMail trash
chuck context inbox mhn9VVBQf outbox Pick6 post4XPUvv postc6DZgu
postnnglMC Rhys sent-mail
chuck@sevenofnine:~/Mail> scan +spam
1 06/08 Woolwich Internet Attention aII Woolwich clients.<<This is a
multi-part message in MIME format.
chuck@sevenofnine:~/Mail> show 1
(Message spam:1)
part 1 text/plain

Does this explain my delemour.We use procmail , fetchmail and sendmail

Cheers


 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-08-2004
Hi again yes the /var/spool/mail/chuck is empty as we process the mail to the
user home dir using MH .
Thus created a mailbox using packf with three emails for testing .

Cheers for pointing this out.


 
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