[reportlab-users] RE: Emailing Corrupting reportlab PDFs
John Pywtorak
reportlab-users@reportlab.com
Fri, 28 May 2004 13:26:02 -0700
On Friday 28 May 2004 03:02 am, Marius Gedminas wrote:
*snip*
> Wouldn't Python's smtplib and email modules solve the problem? You can
> send correctly MIME-encoded email directly instead of scripting Outlook.
>
> Marius Gedminas
Here is how I do it, which works 99% of the time. Python makes it a snap. If
there is some improvement, please feel free to share.
def email_pdf(file, address):
from cStringIO import StringIO
from email.Message import Message
from email.Generator import Generator
import smtplib
import base64
import quopri
msg = Message()
msg.add_header("From", FROM)
msg.add_header("To", address)
msg.add_header("Subject", "YOUR SUBJECT")
msg.add_header("MIME-Version", "1.0")
msg.add_header("Content-Type", "multipart/mixed")
msg.preamble = "Unable to read this email, please upgrage your mail
client\n"
msg.epilogue = "" # Ensure the message ends with a newline.
# Text, or the body of the message attachment.
body = Message()
body.add_header("Content-Type", "text/plain", charset = "us-ascii")
body.add_header("Content-Transfer-Encoding", "quoted-printable")
body.set_payload(quopri.encodestring("YOUR BODY MSG", 1))
msg.attach(body)
# The PDF Audit attachment.
pdfm = Message()
pdfm.add_header("Content-type", "application/pdf", name = file)
pdfm.add_header("Content-transfer-encoding", "base64")
pdfe = StringIO()
try:
pdf = open(file, "rb")
base64.encode(pdf, pdfe)
finally:
pdf.close()
pdfm.set_payload(pdfe.getvalue())
msg.attach(pdfm)
addr_list = [ address, ]
try:
smtp = smtplib.SMTP("smtp.server.com")
smtp.sendmail(
"YOUR FROM ADDRESS",
addr_list,
msg.as_string()
)
finally:
smtp.quit()