THE GOAL: I need to send an email with a simple ASCII text body and an attached HTML file.
I have scripts that send basic emails via the smtplib module that don't have any attachements and that seems to work fine. I first looked at the mimetools modules but it says it is depreceated since 2.3, so I started trying to use the email module. Here is a script that basically follows the second example given in section 7.1.13 of the Python Library Reference (http://docs.python.org/lib/node162.html). (***Note that there are some mistakes in that documentation about module names.***) When I run this and view the email I receive in MS Outlook Express, what I see is the HTML rendered in the body of the message, my body is not seen anywhere and there is no attachment. (Note: I like to bash MS as much as anyone: the reality is I need to work with this client - save it) I have not read the whole spec for RFC 822 and don't profess to understand it, but that's the point of having modules like email, right? Am I misunderstanding something about how I am using 'email' here or is this module not working right? Does this give you an email with an HTML file attachement on your client? Can someone kindly point out what I've done wrong or give me a working example? Thanks! -ej Here's the script (put in your own email and server if you want to run it), and the text (with some identifying info stripped) of the email I receive is below that: #!/usr/bin/env python import smtplib from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart # GLOBAL DATA #============= MAIL_SERVER = 'your_server.com' MAIL_SUBJECT = "Python.SMTP email test" MAIL_TO = '[EMAIL PROTECTED]' MAIL_FROM = "Python.SMTP email test" # Create a text/plain message Body = """\ This is intended to be the body of my email. The HTML below should not be seen directly in the body but should be a separate attachment. """ msg = MIMEMultipart() msg['Subject'] = MAIL_SUBJECT msg['From'] = MAIL_FROM msg['To'] = MAIL_TO msg.preamble = Body html = """\ <html> <head> <title>Sample HTML File</title> </head> <body> <h1>Can you see this?</h1> <p>This is a short paragraph.</p> </body> </html> """ msg.attach(MIMEText(html, 'html')) # print msg.as_string() # Send the message via our own SMTP server, but don't include the # envelope header. smtp = smtplib.SMTP(MAIL_SERVER) smtp.sendmail(MAIL_FROM, [MAIL_TO], msg.as_string()) smtp.close() # end of python script Here's the text of the email I received: Return-Path: <Python.SMTP> Delivered-To: [EMAIL PROTECTED] <several Received headers stripped> Content-Type: multipart/mixed; boundary="===============1669450343==" MIME-Version: 1.0 Subject: Python.SMTP email test From: Python.SMTP email test To: [EMAIL PROTECTED] This is intended to be the body of my email. The HTML below should not be seen directly in the body but should be a separate attachment. --===============1669450343== Content-Type: text/html; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit <html> <head> <title>Sample HTML File</title> </head> <body> <h1>Can you see this?</h1> <p>This is a short paragraph.</p> </body> </html> --===============1669450343==-- -- http://mail.python.org/mailman/listinfo/python-list