def attach(self, attachments): if attachments: for attachment in attachments: if not os.path.isfile(attachment): print 'WARNING: Unable to attach %s because it is not a file.' % attachment continue ctype, encoding = mimetypes.guess_type(attachment) if ctype is None or encoding is not None: ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) fp = open(attachment, 'rb') attachment_mime = MIMEBase("application", "octet-stream") attachment_mime.set_payload(fp.read()) fp.close() Encoders.encode_base64(attachment_mime) attachment_mime.attadd_header('Content-Disposition', 'attachment', filename=attachment) self.body.attach(attachment_mime)