[Tutor] sending html and text emails

Kent Johnson kent37 at tds.net
Thu Dec 11 04:18:37 CET 2008


On Wed, Dec 10, 2008 at 10:18 AM, James <jtp at nc.rr.com> wrote:
> All,
>
> I'm trying to figure out a way to send html email using Python. I've
> found scripts such as the one in the link below:
>
> http://www.stevecoursen.com/2008/10/sending-html-email-from-python.html/
>
> These don't seem to work well. I took a (very) basic html page and had
> the script in the page above read that page and then set up the text /
> html email. When my client received the message it was filled with
> visible html tags.
>
> The ultimate goal is to have a script that displays a simple HTML
> table that's easily readable by anyone using a client capable of
> interpreting html emails.

It might help to see your code, if it is different at all from what is
on that page. What was printed?

Here is a redacted version of a function that worked for me. This
sends HTML only - no text part:

from email.mime.text import MIMEText
from email.Utils import formatdate

def emailReport():

    report = ... # Get HTML text from somewhere

    msg = MIMEText(report, 'html', 'utf-8')

    msg['Subject'] = 'report'
    msg['From'] = 'me at example.com'
    msg['To'] = 'you at example.com'
    msg['Date'] = formatdate()

    server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
    server.sendmail('me at example.com', 'you at example.com', msg.as_string())

    try:
        server.quit()
    except:
         return

Kent


More information about the Tutor mailing list