473,473 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3751
>>>>> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dont bother | last post by:
Hey, I have been trying to parse emails: But I could not find any examples or snippets of parsing emails in python from the documentation. Google did not help me much too. I am trying to...
4
by: Paul Schmidt | last post by:
Dear list: I am new to python, and I am trying to figure out the short answer on something. I want to open a POP3 mailbox, read the enclosed mail using the POP3 module, , and then process it...
0
by: chuck amadi | last post by:
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()...
0
by: Chuck Amadi | last post by:
fp = file("/home/chuck/pythonScript/testbox") mbox = mailbox.UnixMailbox(fp, email.message_from_file) # list of body messages. bodies = # mail is the file object for mail in mbox: print...
1
by: Arenz, Ralph | last post by:
hi all, my problem is to get a tiff-file-attachment out of an email sent by a fax-server. When i try this with "get_payload(decode='True')" i get additional informations, looks like...
2
by: brettk | last post by:
Hello All, Here's what I'm trying to do: I need to connect to a pop3 server, download all messages, and copy all of the attachments into a specific directory. The actual email message is...
19
by: 叮叮当当 | last post by:
hi, all when a email body consist with multipart/alternative, i must know when the boundary ends to parse it, but the email lib have not provide some function to indicate the boundary end,...
7
by: erikcw | last post by:
Hi all, I'm trying to extract zip file (containing an xml file) from an email so I can process it. But I'm running up against some brick walls. I've been googling and reading all afternoon, and...
5
by: Robert Latest | last post by:
Hello, I'm new to Python but have lots of programming experience in C, C++ and Perl. Browsing through the docs, the email handling modules caught my eye because I'd always wanted to write a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.