[smtplib]: How to send binary file attachement ?

Michael P. Reilly arcege at shore.net
Thu Mar 2 21:15:25 CET 2000


Roger Lam <rogerlam replace_with_previous_token at email.com> wrote:
: anyone got a working sample code?

You can look at the mimecntl module at
<URL: http://starship.python.net/crew/arcege/modules/>.

Could go something like:

import smtplib, mimecntl

from_address = 'arcege at shore.net'
sendto_address='my.mom at home.net'

# make the attachment; I read it all in at once to get the size,
# but this is not necessary at all
image = open('arcege.jpg', 'rb').read()
mimedoc = mimecntl.MIME_recoder(
  image,
  # the content fields
  ( mimecntl.MIMEField('content-type', 'image/jpg', name='arcege.jpg'),
    mimecntl.MIMEField('content-length', len(image)),
    mimecntl.MIMEField('content-disposition', 'attachment',
                       filename='arcege.jpg')
  )
)

# encode it
mimedoc.encode('base64')

# not make the mail message
mailmsg = mimecntl.MIME_document(
  mimedoc,
  from=from_address,
  to=sendto_address,
  subject="Here is a picture of me"
)

smtpconn = smtplib.SMTP('localhost')
smtpconn.sendmail(
  from_address,
  (sendto_address,),
  str(mailmsg)
)
smtpconn.quit()

Hope this helps,
  -Arcege




More information about the Python-list mailing list