MimeWriter and Excel file

juan Carlos Coruña jcoruna at umd.es
Mon Jun 5 13:34:32 EDT 2000


Hello all,
I have the following problem:
I send an email with python using the MimeWriter module and an excel 
file attached. When I send the to myself inside our intranet the 
attchmente arrives well, but if I send the email to anyone outside our 
intranet and open the attachment with MS Excel I see the base64 
encoded characters. Any ideas.
Here is the source:
#!/usr/bin/python

import time, string, StringIO, MimeWriter, base64, os
from smtplib import SMTP

def enviarcorreo(cliente):
    msjhtml = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 
Transitional//EN">\n' + \
	      '<HTML><HEAD>\n' + \
	      '<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; 
=charset=3Diso-8859-1">\n\n' + \
	      '<META content=3D"MSHTML 5.00.2014.210" 
name=3DGENERATOR></HEAD>\n' + \
	      '<BODY bgColor=3D#ffffff>\n' + \
	      '<DIV><FONT face=3DArial size=3D2><SPAN 
class=3D400082209-17022000>' + \
	      'This is the body' + '</SPAN></FONT></DIV>' + \
	      '<DIV><FONT face=3DArial size=3D2><SPAN 
class=3D400082209-17022000>' + \
	      '</SPAN></FONT></DIV></BODY></HTML>'
    msj = 'This is the body'
    
    # inicializar mensaje
    outputfp = StringIO.StringIO()
    w = MimeWriter.MimeWriter(outputfp)
    w.addheader('From', '<jcc at umd.es>') 
    w.addheader('To', '<jcc at umd.es>')
    w.addheader('Subject', 'test')
    w.addheader('MIME-Version', '1.0')
    w.flushheaders()
    h = w.startmultipartbody('mixed')
    h.write('This is a multi-part message in MIME format.\n')
    sw = w.nextpart()
    sw.startmultipartbody('alternative')
    
    ssw = sw.nextpart()
    ssw.addheader('Content-Transfer-Encoding', '7bit')
    ssw.flushheaders()
    f = ssw.startbody('text/plain', [('charset', 'iso-8859-1')])
    f.write(msj)
    
    ssw = sw.nextpart()
    ssw.addheader('Content-Transfer-Encoding', 'quoted-printable')
    ssw.flushheaders()
    f = ssw.startbody('text/html', [('charset', 'iso-8859-1')])
    f.write(msjhtml)
    sw.lastpart()
    
    
    sw = w.nextpart()
    sw.addheader('Content-Transfer-Encoding', 'base64')
    sw.addheader('Content-Disposition', 'attachment; filename="List de 
precios UMD.xls"')
    sw.flushheaders()
    f = sw.startbody('application/x-msexcel', [('name', 'Lista de 
precios UMD.xls')])
    try:
        base64.encode(open('filename.xls', 'rb'), f)
    except IOError, error:
	print 'Error: %s. No enviado %s a %s' % (error, 
lista[cliente.field('Columnas_precios')], 
cliente.field('Correo_elect___precios_'))
	return
    w.lastpart()
    correo = SMTP('server.bio.umd.es')
    correo.set_debuglevel(0)

    correo.sendmail(fromaddr, 'Juan Carlos Coruña <jcc at umd.es>', 
outputfp.getvalue())
    correo.quit()







More information about the Python-list mailing list