Sending HTML email using Python

前端 未结 10 1268
小鲜肉
小鲜肉 2020-11-22 04:57

How can I send the HTML content in an email using Python? I can send simple text.

10条回答
  •  故里飘歌
    2020-11-22 05:28

    Here's sample code. This is inspired from code found on the Python Cookbook site (can't find the exact link)

    def createhtmlmail (html, text, subject, fromEmail):
        """Create a mime-message that will render HTML in popular
        MUAs, text in better ones"""
        import MimeWriter
        import mimetools
        import cStringIO
    
        out = cStringIO.StringIO() # output buffer for our message 
        htmlin = cStringIO.StringIO(html)
        txtin = cStringIO.StringIO(text)
    
        writer = MimeWriter.MimeWriter(out)
        #
        # set up some basic headers... we put subject here
        # because smtplib.sendmail expects it to be in the
        # message body
        #
        writer.addheader("From", fromEmail)
        writer.addheader("Subject", subject)
        writer.addheader("MIME-Version", "1.0")
        #
        # start the multipart section of the message
        # multipart/alternative seems to work better
        # on some MUAs than multipart/mixed
        #
        writer.startmultipartbody("alternative")
        writer.flushheaders()
        #
        # the plain text section
        #
        subpart = writer.nextpart()
        subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
        pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
        mimetools.encode(txtin, pout, 'quoted-printable')
        txtin.close()
        #
        # start the html subpart of the message
        #
        subpart = writer.nextpart()
        subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
        #
        # returns us a file-ish object we can write to
        #
        pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
        mimetools.encode(htmlin, pout, 'quoted-printable')
        htmlin.close()
        #
        # Now that we're done, close our writer and
        # return the message body
        #
        writer.lastpart()
        msg = out.getvalue()
        out.close()
        print msg
        return msg
    
    if __name__=="__main__":
        import smtplib
        html = 'html version'
        text = 'TEST VERSION'
        subject = "BACKUP REPORT"
        message = createhtmlmail(html, text, subject, 'From Host ')
        server = smtplib.SMTP("smtp_server_address","smtp_port")
        server.login('username', 'password')
        server.sendmail('sender@host.com', 'target@otherhost.com', message)
        server.quit()
    

提交回复
热议问题