MimeWriter and PDFs

Brandon berklee at hotmail.com
Fri May 2 06:52:23 EDT 2003


Hoping I can get a little help here...

I've got a global nested list of recipients, each containing a file
name and an e-mail address. The process that generates the PDFs has no
problem - I'm saving them to the hard drive, and opening them seems to
not be a problem.

However, once I attach the PDF and send it, I try to open the attached
file and get the following message from Acrobat:

"There was an error opening this document. The file is damaged and
could not be repaired."

I've opened the PDF in a text viewer (the same PDF, before and after
sending it), and it appears that the one that's been sent out has a
bunch of extra data on the end of it.

Anyone? Anyone? Bueller? :)

Thanks,
B.

def shipEMail():
    """
    Sends MIME-Type e-mails with attached PDFs, and deletes them
afterwards.
    """
    fName = "75day.pdf"
    for eMail in recipients:
        # We are using a string as a temporary file
        outputfp = StringIO.StringIO()
        w = MimeWriter.MimeWriter(outputfp)
        w.addheader("subject", "Report")
        w.addheader("MIME-Version", "1.0")
        w.flushheaders()
        w.startmultipartbody("mixed")
        instructions = w.nextpart()
        instFile = instructions.startbody("text/plain")
        instructions.flushheaders()
        instFile.write("""
Please see the attached PDF file.
""")
        subwriter = w.nextpart()
        subwriter.addheader("Content-Transfer-Encoding", "base64")
        subwriter.addheader("Content-Disposition", 'attachment;
filename="%s"' % fName)
        f = subwriter.startbody('application/octet-stream; name="%s"'
% fName)
        #f = subwriter.startbody('application/pdf; name="%s"' % fName)
        subwriter.flushheaders()
        base64.encode(open('%s' % eMail[1], 'r'), f)
        w.lastpart()
        try:
            server = smtplib.SMTP(mailServer)
            result = server.sendmail("someguy at somedomain.com",
eMail[0], outputfp.getvalue())
            server.quit()
            if result:
                for r in result.keys():
                    print "Error sending to ", r
                    rt = result[r]
                    print "Code : ", rt[0], ":", rt[1]
            else:
                print "Sent to " + eMail[0] + " successfully."
        except (smtplib.SMTPException, socket.error), arg:
            print "SMTP Server could not send mail", arg




More information about the Python-list mailing list