Python 1.5.2 - Multipart Mime handling.
james
jamesdean at hotmail.com
Fri May 9 13:08:52 EDT 2003
Ok thank you for your help this morning I have made some progress, but
I am still having problems and was wondering if any python guru's
would tell me what I am doing wrong.
Below is the code I have come up with. (Please excuse the messy code
this is a learning experience I'll make it nicer and remove all the
print statments once it works)
I'm getting this error: (and i'm not sure why! can someone help me? Is
what I am trying to do possible?)
Traceback (innermost last):
File "MAILIN2.py", line 185, in ?
rawtext = mf.read()
File "/usr/lib/python1.5/multifile.py", line 118, in read
return string.joinfields(self.readlines(), '')
File "/usr/lib/python1.5/multifile.py", line 112, in readlines
line = self.readline()
File "/usr/lib/python1.5/multifile.py", line 93, in readline
if marker == self.section_divider(sep):
File "/usr/lib/python1.5/multifile.py", line 157, in section_divider
return "--" + str
TypeError: illegal argument type for built-in operation
#Code out line
#if message piped into stdin is multipart decode & extract all parts
to files
#if just one part plain text extract to file
#read in from stdin
mailheaders = mimetools.Message(sys.stdin)
mailbody = sys.stdin.read()
#set up part counter
#partno = 1
#get Content-Type
contenttype =
mailheaders.getheader('Content-Type', 'text/plain')
#set contenttype to force entering if statment
#contenttype = "multipart/"
if contenttype[:10] ==("multipart/"):
#set up string buffer
mailstream = StringIO(mailbody)
print "MAILSTREAM =>",mailstream
mimemsg = mailheaders
print "mimemsg =>", mimemsg
#get boundary
boundary = mimemsg.getparam("boundary")
print "Boundry =>", boundary
#make pointer in buffer
mf = multifile.MultiFile(mailstream)
print "mf =>", mf
#push boundary string in buffer
mf.push(boundary)
while mf.next():
print "HERE"
msgpart = mimetools.Message(mf)
print "msgpart => ", msgpart
subtype = msgpart.gettype()
print "subtype=>",subtype
###Line 185 is next
rawtext = mf.read()
print "rawtext=>",rawtext
if subtype == 'text/plain':
# save as text file
name = msgpart.get_param("name")
if name==None:
name = "part-%i" % partno
f=open(name,"wb")
f.write(msg.part.get_payload(decode=1))
f.close()
print name, "extracted to file"
partno = partno + 1
else:
name = msgpart.get_param("name")
if name==None:
name = "part-%i" % partno
f=open(name,"wb")
f.write(msgpart.get_payload(decode=1))
f.close()
print name, "extracted to file"
partno = partno + 1
#Else if Content-Type is text/plain, get the body
text
elif contenttype[:10] == "text/plain":
bodytext = msg.fp.read()
else:
bodytext = 'no body text'
More information about the Python-list
mailing list