SMTPlib Emailing Attachments

Bill bblancett at
Wed Sep 17 18:49:01 CEST 2003

I am trying to have the capability to email attachments. Specifically
I want to be able to email a specific attachment that I name that may
be a PDF document, text doc, etc. I already have a working piece of
code that emails jpg attachments but does not work with any other
types of attachments. Could someone tell me how to modify this code to
send other types of attachments like the one's stated above(especially
PDF's)? Also how do I determine the name of the attachment? Right now
it defaults to Attach0. Here is the current code I have below:
import sys, smtplib, MimeWriter, base64, StringIO

message = StringIO.StringIO()
writer = MimeWriter.MimeWriter(message)
writer.addheader('Subject', 'The Text test')

# start off with a text/plain part
part = writer.nextpart()
body = part.startbody('text/plain')
body.write('This is a picture of chess, enjoy :)')

# now add an image part
part = writer.nextpart()
part.addheader('Content-Transfer-Encoding', 'base64')
body = part.startbody('image/jpeg')
#body = part.startbody('text/plain')
base64.encode(open('c:\email\chess01.jpg', 'rb'), body)

# finish off

# send the mail
smtp = smtplib.SMTP('')
smtp.sendmail('bblancett at', 'bblancett at',

More information about the Python-list mailing list