473,414 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 2491
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David McBride | last post by:
I'm getting the following message when I run my Perl script that uses the XML::Writer module: "Processing instruction target begins with 'xml' at .../makeconf/LW/LWTest.pm line 75" The pi()...
6
by: RCS | last post by:
I've been running into more and more complexity with an application, because as time goes on - we need more and more high-level, rolled-up information. And so I've created views, and views that use...
4
by: dixie | last post by:
I came across a lot of repetitious code in a form module that I would like to put in a module that I can call multiple times from a form. The problem I have run into is that when I put the code...
1
by: Venky | last post by:
I'm compiling a C program that is using Interbase 6.0 APIS. Getting the following errors at the time of linking. Linking test.exe: Linker Warning: No module definition file specified: using...
5
by: pyapplico | last post by:
Is there any possible way that I can place a .py file on the internet, and use that source code in an .py file on my computer?
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
1
by: openuser | last post by:
Hello, I've been researching how to embed python into C/C++. And, I learned how to write c/c++ code that, in threory, should do its job in embedding Python module into itself. Here is what I have.....
4
by: Jean-Claude Neveu | last post by:
Hello, I am writing a Python program to check email using POP3. I've tried the sample code from python.org, and it works great. In other words, the code below successfully prints out my emails....
11
by: minishilpi | last post by:
I have a question - I have this code below to send an email and I have referenced the Microsoft CDO Library 2000. It doesn't throw any exception in the console window. It goes throughout the code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.