Python email PDF: Some PDFs Getting Corrupted -
i trying attach pdf file e-mail message.
for 1 pdf (a word document printed pdf), works (the recipient opens in outlook no problem).
yet other pdfs (which seem same except being few kbs larger), corrupted.
here sample use fails (becomes corrupted).
import smtplib, os email.mime.multipart import mimemultipart email.mime.base import mimebase email.mime.text import mimetext email.mime.application import mimeapplication email.utils import formatdate email import encoders attachment_path=r'c:\directory'+'\\' login='login' password='password' part=mimebase('application',"octet-stream") def message(attachment): #attachment pdf file name fromaddr = "example@example.com" cc=fromaddr msg = mimemultipart() msg['from'] = fromaddr msg['to'] = "example@example.com" msg['date'] = formatdate(localtime = true) msg['subject'] = "subject" body=''' <!doctype html> <html> <body> <p><font face="tahoma" size=2> hope going well.</p></font> </body> </html> ''' msg.attach(mimetext(body, 'html')) part.set_payload(open(attachment_path+attachment,'rb').read()) encoders.encode_base64(part) part.add_header('content-disposition', 'attachment; filename="{0}"'.format(os.path.basename(attachment_path+attachment))) msg.attach(part) mail=smtplib.smtp('server',587) mail.ehlo() mail.starttls() mail.login(login,password) mail.sendmail(fromaddr,[toaddr,cc],msg.as_string())
i have tried using following instead of base 64 encoding, no avail:
encoders.encode_noop(part) encoders.encode_7or8bit(part) encoders.encode_quopri(part)
thanks in advance!
all had move this:
part=mimebase('application',"octet-stream")
to above:
part.set_payload(open(attachment_path+attachment,'rb').read())
Comments
Post a Comment