mime email creation with attachment

Rene rj.nederhand at pg.tno.nl
Mon Oct 18 13:00:48 EDT 1999


Dear all,

I try to automatically create an email message containing an attachment.
Untill now I created the following code, but the format it sends can't be
read by outlook 98 (it doesn't do uudecode, while it should be able to do
that).
Is there something wrong with my code (I am just a newbie) or is it
something with outlook?

Thanks in advance,
Rene

My code:
import uu, base64
import sys
import StringIO
import MimeWriter
from smtplib import SMTP

# Mails the report file to the user in MIME format
# Based upon code by GVR
def mailFileToUser(_userName, _fileName):
       outputfp = StringIO.StringIO()
       w = MimeWriter.MimeWriter(outputfp)
       w.addheader("subject", "Strip studie rapport")
       w.flushheaders()
       w.startmultipartbody("mixed")
       subwriter = w.nextpart()
       f = subwriter.startbody('application/octet-stream;name="Strip.xls"')

       subwriter.addheader("Content-Transfer-Encoding", "uuencode")
       subwriter.addheader("Content-Disposition",
'attachment;filename="Strip.xls"')
       subwriter.flushheaders()
       uu.encode(open('./test.xls', 'r'),f)
       w.lastpart()
       s = SMTP("a00677.pg.tno.nl")
       s.sendmail("rj.nederhand at pg.tno.nl",
["rj.nederhand at pg.tno.nl"],outputfp.getvalue())
       s.close()
       print outputfp.getvalue()
if __name__=='__main__':
       mailFileToUser('rj.nederhand at pg.tno.nl', 'TestFile.out')







More information about the Python-list mailing list