[issue9298] binary email attachment issue with base64 encoding

2010-07-19 Thread Vance Unruh

Vance Unruh vun...@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 m...@myprovider.net'
msg['To'] = 'You y...@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 rep...@bugs.python.org
http://bugs.python.org/issue9298
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9298] binary email attachment issue with base64 encoding

2010-07-18 Thread Vance Unruh

New submission from Vance Unruh vun...@earthlink.net:

I'm using Python to email a text version and a PDF version of a report. The 
standard way of doing things does not work with Vista's Mail program, but works 
fine with Mail on OSX. So, I don't know if this is a Python or a Vista Mail 
bug. By standard way, I mean:

# Add the attached PDF:
part = MIMEApplication(pdf,pdf)
part.add_header('Content-Disposition', 'attachment', filename=pdfFile)
msg.attach(part)


To fix the problem, I changed C:\Python31\Lib\email\encoders.py to use 
encodebytes instead of b64encode in order to get mail on Windows Vista to 
correctly interpret the attachment. This splits the base64 encoding into many 
lines of some fixed lenth.

I can achieve the same thing adding the attachment by hand with the following 
code:

from email.mime.base import MIMEBase
part = MIMEBase(application,pdf)
part.add_header('Content-Transfer-Encoding', 'base64') 
part.set_payload(str(base64.encodebytes(pdf),'ascii'))
msg.attach(part)

Seems like I shouldn't need to know this much.

I'm new to Python and this is the first bug I have submitted, so if you need 
additional information, please let me know.

--
components: Library (Lib)
messages: 110710
nosy: vunruh
priority: normal
severity: normal
status: open
title: binary email attachment issue with base64 encoding
versions: Python 3.1

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9298
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com