using the email module

E

Erik Johnson

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 = '(e-mail address removed)'
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: (e-mail address removed)
<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: (e-mail address removed)

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

Sybren Stuvel

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
 
E

Erik Johnson

Sybren Stuvel said:
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! ;)
 
D

Dennis Lee Bieber

msg.preamble = Body
From the help system:

preamble
The format of a MIME document allows for some text between the blank
line following the headers, and the first multipart boundary string.
Normally, this text is never visible in a MIME-aware mail reader because
it falls outside the standard MIME armor. However, when viewing the raw
text of the message, or when viewing the message in a non-MIME aware
reader, this text can become visible.

Note the sentence beginning "Normally, ..."


Suggest you try

msg.attach(MIMEText(body, "plain"))

to create the first MIME "part"
msg.attach(MIMEText(html, 'html'))

Two (three?) step this (I hope the calls are valid, I haven't
checked the full inheritance chain)

attachment = MIMEText(html, "html")
attachment.add_header("content-disposition",
"attachment",
"filename='something.html' ")
msg.attach(attachment)

You need to get the content-disposition added the the message PART
(the attachment) to tell the client how to interpret that part.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top