SMTPlib Emailing Attachments

Bill bblancett at hotmail.com
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')
writer.startmultipartbody('mixed')

# 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
writer.lastpart()

# send the mail
smtp = smtplib.SMTP('fc.hbu.edu')
smtp.sendmail('bblancett at hbu.edu', 'bblancett at hbu.edu',
message.getvalue())
smtp.quit()
__________________________________________________________________________




More information about the Python-list mailing list