By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

Send HTML e-mail in Python?

P: n/a
Max
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>.
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
mk***@yahoo.com (Max) writes:
Simply sending a text message with HTML tags does not work. The e-mail
client displays the message as plain text with visible <tags>.


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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #2

P: n/a
Max wrote:
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>.


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
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Jul 18 '05 #3

P: n/a
Google is your friend: python send htlm email

turns up a recipe at ASPN:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/67083

Larry Bates
Max wrote:
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>.

Jul 18 '05 #4

P: n/a
Max
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(smtpserver)
smtp.sendmail(mfrom, mto, msg)

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.