470,647 Members | 1,268 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

Howto use email module and write the get_payload to a file

Hi I have managed to print the output of the get_payload to screen
but I need to write to a file as I only require the email body messages
from the mailbox.My script using the fp.readlines() function writes the
entire contents of the mailbox of cause including the headers of the
emails I do not want.

I have tried a few things but I cant get to my goal.Any ideas or
pointers I need only the email body and I cant figute out why I can
using the print statement but get those results to a file.
cheers

import sys
import os
import email
import mailbox
import StringIO
fp = file ("/var/spool/mail/chucka")
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()
mailout = file("/home/chucka/pythonScript/SurveyResults1.txt","r")
fp = open("/var/spool/mail/chucka")
mb = mailbox.UnixMailbox(fp, email.message_from_file)

#for bdymsg in fp.xreadlines():
#for bdymsg in fp.readlines():
#for msgbodies in mb:
# mailout.write(bdymsg)
# bdymsg = mail.get_payload()
# mailout.write(mail.get_payload()

for bmsg in mb:
bmsg = get_payload()
mailout.write(bmsg)
# bmsg = [get_payload]
print "mailbox file copied...to SurveyResults.txt"

# Now close the files
mailout.close()

Jul 18 '05 #1
1 3376
>>>>> chuck amadi <ch*********@ntlworld.com> (CA) wrote:

CA> Hi I have managed to print the output of the get_payload to screen
CA> but I need to write to a file as I only require the email body messages
CA> from the mailbox.My script using the fp.readlines() function writes the
CA> entire contents of the mailbox of cause including the headers of the
CA> emails I do not want.

CA> mailout = file("/home/chucka/pythonScript/SurveyResults1.txt","r")

If you open the file with "r" you can't write to it.

CA> fp = open("/var/spool/mail/chucka")
CA> mb = mailbox.UnixMailbox(fp, email.message_from_file)
CA> for bmsg in mb:
CA> bmsg = get_payload()

You use bmsg for two purposes: as the iteration variable, and to get the
payload. Moreover get_payload is a method and hence needs an object.

for bmsg in mb:
msgb = bmsg.get_payload()
mailout.write(msgb)

But that doesn't take into account that the payload will be a list when
the message is multipart. In that case you need some more elaborate code
like:
def writebody(mailout, msg):
payld = msg.get_payload()
if msg.is_multipart():
for m in payld:
writebody(mailout, m)
else:
mailout.write(payld)

for bmsg in mb:
writebody(mailout, bmsg)
print "mailbox file copied...to SurveyResults.txt"

--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@hccnet.nl
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by dont bother | last post: by
4 posts views Thread by Paul Schmidt | last post: by
1 post views Thread by Arenz, Ralph | last post: by
19 posts views Thread by 叮叮当当 | last post: by
7 posts views Thread by erikcw | last post: by
5 posts views Thread by Robert Latest | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.