473,799 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ 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.UnixMai lbox(fp, email.message_f rom_file)
# list of body messages.
bodies = []

# mail is the file object
for mail in mbox:
print 'mail'
print mail['Subject']
print mail.get_conten t_type()#text/plain
print mail.get_payloa d()
mailout = file("/home/chucka/pythonScript/SurveyResults1. txt","r")
fp = open("/var/spool/mail/chucka")
mb = mailbox.UnixMai lbox(fp, email.message_f rom_file)

#for bdymsg in fp.xreadlines() :
#for bdymsg in fp.readlines():
#for msgbodies in mb:
# mailout.write(b dymsg)
# bdymsg = mail.get_payloa d()
# mailout.write(m ail.get_payload ()

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

# Now close the files
mailout.close()

Jul 18 '05 #1
1 3782
>>>>> chuck amadi <ch*********@nt lworld.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.UnixMai lbox(fp, email.message_f rom_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_payloa d()
mailout.write(m sgb)

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(mailo ut, msg):
payld = msg.get_payload ()
if msg.is_multipar t():
for m in payld:
writebody(mailo ut, m)
else:
mailout.write(p ayld)

for bmsg in mb:
writebody(mailo ut, bmsg)
print "mailbox file copied...to SurveyResults.t xt"

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3580
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 understand the module 'email' and the functions described there to parse email but seems difficult. Can anyone help me in locating some pointers or snippets on this issue.
4
3735
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 using the email module. Environment Python 2.3.4, Mandrake Linux 9.0 patched up the wazoo...
0
1328
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() 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...
0
1027
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 mail print mail.get_content_type()#text/plain
1
1433
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 binary-mixed header-information, enveloping the tiff-data. I tried the following: Sending the email from outlook to a postfix-user and a file on the postfix-host as well. The file shows the stucture mentioned
2
6737
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 unimportant. Now, I've found plenty of examples that strip the attachments from an email message, but most (if not all) of them take a file parameter. My question is this:
19
2600
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, how to solve it ? thanks.
7
7650
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 can't seem to figure it out. Here is what I have so far. p = POP3("mail.server.com")
5
1275
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 script to handle my huge, ancient, and partially corrupted email archives. Of course I know that this kind of project shouldn't be tackled by a beginner in a language, but I still thought I'd give it a spin.
0
9689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10495
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.