[Zope] Problems decoding MIME parts with CMFMail script "addtoMailFolder_unpack_part"

Michael LaPera mlapera@colorbyte.com
Thu, 13 Jun 2002 14:11:55 -0400


I am using CMFMail to bring email into Zope Members accounts (using CMF 
1.2, and QMail).  Everything works great if I just send ONLY plain text 
messages, put if I send multipart/mixed messages CMFMail chokes and I 
get a error (in my QMail error logs) that basically says "Problem 
Connecting to server EOF when reading a line."

I tracked it down to this fairly standard MIME decoder  
"addtoMailFolder_unpack_part " (shown below) which is being called by 
the main script.  It is choking is when gets to the 
mimetools.copyliteral(file,outfile) or 
mimetools.decode(file,outfile,encoding) at the bottom of the 
script  --- >> see code below.

I tried testing for specific and general exceptions, but none where 
thrown (at least that I could catch).  I commented out the offending 
mimetool code and printed the file to check the results ---- they look 
just fine.

Does anyone have ANY suggestions.  I'm not really sure what to do next 
(and I really need to be able to get to the decoded mime parts).  If you 
need more detail let me know...


--Michael LaPera

++++++++++++++++++++++++++++++++++++++++++

## Script (Python) "addtoMailFolder_unpack_part"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=theMail,file,partno
##title=Add man to Mail Folder (unpack_part)
##

self = context

import mimetools
import StringIO

headers = mimetools.Message(file)
type = headers.getmaintype()
if type == 'multipart':
     boundary = headers.getparam("boundary")
     file.push(boundary)
     file.readlines()
     while not file.last:
         file.next()
         partno = partno + 1
         self.addtoMailFolder_unpack_part(theMail, file, partno)
     file.pop()
     return

name = headers.getparam("name")
subtype = headers.getsubtype()
if type=='text' and subtype=='plain' and not name:
     # overwrite the body so that we get _only_ the body text.
     # (Uses the plain version of the body in all situations.
     #  HTML formated body will be in the attachments dictionary)
     if theMail['body'] != '':
         theMail['body'] = theMail['body'] + '\n--body--\n'
     theMail['body'] = theMail['body'] + file.read()
else:
     if not name:
         name = 'part%d' % (partno,)
         name = name + '-'+subtype

     encoding = headers.getencoding()

     outfile = StringIO.StringIO()
     if encoding == '7bit':
         mimetools.copyliteral(file,outfile)     ###  PROBLEM AREA
     else:
         mimetools.decode(file,outfile,encoding)     ###  PROBLEM AREA

     theMail['attachments'][name]=outfile.getvalue()
     outfile.close()