SMTPlib Emailing Attachments

Rudy Schockaert rudy.schockaert at
Wed Sep 17 20:27:54 CEST 2003

Bill wrote:
> 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('')
> smtp.sendmail('bblancett at', 'bblancett at',
> message.getvalue())
> smtp.quit()
> __________________________________________________________________________

Wouldn't it be a lot easier to use the email module?

This is a snippet of how I send attachments (simply replace 
TO,FROM,Subect, path (full pathname including filename) and filename 
with relevant data):

import smtplib
import mimetypes
from email.Encoders import encode_base64
from email.MIMEAudio import MIMEAudio
from email.MIMEBase import MIMEBase
from email.MIMEImage import MIMEImage
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

def getAttachment(path, filename):
     ctype, encoding = mimetypes.guess_type(path)
     if ctype is None or encoding is not None:
         ctype = 'application/octet-stream'
     maintype, subtype = ctype.split('/', 1)
     fp = open(path, 'rb')
     if maintype == 'text':
         attach = MIMEText(,_subtype=subtype)
     elif maintype == 'message':
         attach = email.message_from_file(fp)
     elif maintype == 'image':
         attach = MIMEImage(,_subtype=subtype)
     elif maintype == 'audio':
         attach = MIMEAudio(,_subtype=subtype)
         print maintype, subtype
         attach = MIMEBase(maintype, subtype)
     attach.add_header('Content-Disposition', 'attachment',
     return attach

msg = MIMEMultipart()
msg['From'] = FROM
msg['To'] = TO
msg['Subject'] = SUBJECT
attach = getAttachment(path, filename)

server = smtplib.SMTP(MAILSERVER)
server.sendmail(FROM, [TO], msg.as_string())

More information about the Python-list mailing list