[issue9298] binary email attachment issue with base64 encoding
Vance Unruh
report at bugs.python.org
Mon Jul 19 16:57:04 CEST 2010
Vance Unruh <vunruh at earthlink.net> added the comment:
Here's code that attaches the pdf file the two different ways. Both attachments are OK when I read the mail on OSX, but one is corrupt when read with Windows Mail on Vista. I wasn't sure what to do with the actual sending of the mail to the server. You'll have to change the code to use your account or something.
def emailReport(pdf):
"""Email the report as multi-part MIME"""
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
msg['Subject'] = 'Corrupt PDF'
msg['From'] = 'Me <me at myProvider.net>'
msg['To'] = 'You <you at yourProvider.com>'
# Add the PDF the easy way that fails:
from email.mime.application import MIMEApplication
fp = open(pdf, 'rb')
part = MIMEApplication(fp.read(),"pdf")
fp.close()
part.add_header('Content-Disposition', 'attachment',filename='This one fails.pdf')
msg.attach(part)
# Add the PDF the hard way using the legacy base64 encoder
from email.mime.base import MIMEBase
part = MIMEBase("application","pdf")
part.add_header('Content-Transfer-Encoding', 'base64')
part.add_header('Content-Disposition', 'attachment',filename='This one works.pdf')
import base64
fp = open(pdf, 'rb')
part.set_payload(str(base64.encodebytes(fp.read()),'ascii'))
fp.close()
msg.attach(part)
# Send the email
from smtplib import SMTP
server = SMTP('smtpauth.provider.net')
server.login(user,password)
server.sendmail(msg['From'], recipient, msg.as_string())
server.quit()
emailReport('bugs.python.org_issue9298.pdf')
----------
Added file: http://bugs.python.org/file18064/bugs.python.org_issue9298.pdf
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9298>
_______________________________________
More information about the Python-bugs-list
mailing list