472,146 Members | 1,216 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

using the email module


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 = 'y********@example.com'
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: my*****@example.com
<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: my*****@example.com

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==--

Sep 28 '06 #1
3 2387
Erik Johnson enlightened us with:
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.
If the HTML document should really be attached, give it a
Content-Disposition: Attachment
header. Check out the accompanying headers as well, by simply emailing
yourself an attached HTML file and examining the email source.

Sybren
--
Sybren Stüvel
Stüvel IT - http://www.stuvel.eu/
Sep 28 '06 #2

"Sybren Stuvel" wrote:
If the HTML document should really be attached, give it a
Content-Disposition: Attachment
header. Check out the accompanying headers as well, by simply emailing
yourself an attached HTML file and examining the email source.
html = """\
<html>
....
</html>
"""
attachment = MIMEText(html, 'html')
attachment['Content-Disposition'] = 'attachment; filename="sample.html"'
msg.attach(attachment)
# Ah! Yes, that works! Thank you! ;)
Sep 28 '06 #3
Erik Johnson enlightened us with:
# Ah! Yes, that works! Thank you! ;)
You're welcome!

Sybren
--
Sybren Stüvel
Stüvel IT - http://www.stuvel.eu/
Sep 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by pyapplico | last post: by
4 posts views Thread by Jean-Claude Neveu | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.