Send HTML e-mail in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Max

    #1

    Send HTML e-mail in Python?

    I am looking for some simple example code on generating SMTP e-mail
    messages in Python. I have a script that sends system reports to me at
    a central e-mail address, and it works fine using smtplib with plain
    text messages. I would like to enhance this script to send HTML
    formatted messages instead.

    Simply sending a text message with HTML tags does not work. The e-mail
    client displays the message as plain text with visible <tags>.
  • Mike Meyer

    #2
    Re: Send HTML e-mail in Python?

    mksql@yahoo.com (Max) writes:
    [color=blue]
    > Simply sending a text message with HTML tags does not work. The e-mail
    > client displays the message as plain text with visible <tags>.[/color]

    You need to add MIME headers to the mail message. I.e., along with the
    From: and Subject: lines, add:

    MIME-Version: 1.0
    Content-Type: text/html

    As long as you're using straight ascii, that should be sufficient. If
    you want to use extended character sets, things get messy. See <URL:
    http://www.cse.ohio-state.edu/cs/Ser.../smtplist.html > for the
    RFCs you'll want to look at.

    <mike
    --
    Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    • Steve Holden

      #3
      Re: Send HTML e-mail in Python?

      Max wrote:
      [color=blue]
      > I am looking for some simple example code on generating SMTP e-mail
      > messages in Python. I have a script that sends system reports to me at
      > a central e-mail address, and it works fine using smtplib with plain
      > text messages. I would like to enhance this script to send HTML
      > formatted messages instead.
      >
      > Simply sending a text message with HTML tags does not work. The e-mail
      > client displays the message as plain text with visible <tags>.[/color]

      You probably didn't include a "Content-Type: text/html" header in the
      RFC822 headers. If that wasn't the problem I do have some code that
      works, but I'd have to strip out client-specific features before I
      posted it.

      regards
      Steve
      --


      Holden Web LLC +1 800 494 3119

      Comment

      • Larry Bates

        #4
        Re: Send HTML e-mail in Python?

        Google is your friend: python send htlm email

        turns up a recipe at ASPN:



        Larry Bates


        Max wrote:[color=blue]
        > I am looking for some simple example code on generating SMTP e-mail
        > messages in Python. I have a script that sends system reports to me at
        > a central e-mail address, and it works fine using smtplib with plain
        > text messages. I would like to enhance this script to send HTML
        > formatted messages instead.
        >
        > Simply sending a text message with HTML tags does not work. The e-mail
        > client displays the message as plain text with visible <tags>.[/color]

        Comment

        • Max

          #5
          Re: Send HTML e-mail in Python?

          The solution was I simply missed putting the correct (and
          aforementioned) content type in the message header:

          msg = ("MIME-Version: 1.0\r\nContent-type: text/html;
          charset=utf-8\r\nFrom: %s\r\nTo: %s\r\nSubject: Test Message\r\n" %
          (mfrom, mto)) + html
          smtp = smtplib.SMTP(sm tpserver)
          smtp.sendmail(m from, mto, msg)

          Comment

          Working...