mime email creation with attachment

Rene rj.nederhand at pg.tno.nl
Tue Oct 19 14:06:05 EDT 1999


Thanks a lot for al the help I receive.

I changed uuencode to base64, use rb now for opening the excel file, and
added mime-version and a to:, but still the files opens as a encoded ascii
document when I try to open it in Excel. I really have no idea what could be
wrong. I hope you do.

Thanks again,

Rene


This is the changed code:

import uu, base64
import sys
import StringIO
import MimeWriter
from smtplib import SMTP

# Mails the Diary Import 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("To", "rj.nederhand at pg.tno.nl") # added this line
       w.addheader("subject", "Strip studie rapport")
       w.addheader("Mime-Version", "1.0")  # added this line
       w.flushheaders()
       w.startmultipartbody("mixed")
       subwriter = w.nextpart()
       f = subwriter.startbody('application/octet-stream;name="Strip.xls"')

       subwriter.addheader("Content-Transfer-Encoding", "base64") # is now
base64
       subwriter.addheader("Content-Disposition",
'attachment;filename="Strip.xls"')
       subwriter.flushheaders()
       base64.encode(open('./test.xls', 'rb'),f)   # changed this part to rb
       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')


Fredrik Lundh heeft geschreven in bericht
<021b01bf1a14$8574ddc0$f29b12c2 at secret.pythonware.com>...
>Rene <rj.nederhand at pg.tno.nl> wrote:
>> 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).
>
>(fwiw, outlook express handles mails from this
>program just fine).
>
>to make it use base64 encoding, just change the
>content type to "base64", and use base64.encode
>instead of uu.encode.
>
>and make sure to add "mime-version", "to", and
>perhaps some other headers as well.
>
>(btw, XLS-files are not plain text files.  use "rb"
>when opening the file).
>
></F>
>
><!-- (the eff-bot guide to) the standard python library:
>http://www.pythonware.com/people/fredrik/librarybook.htm
>-->
>
>
>






More information about the Python-list mailing list