473,728 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 misunderstandin g 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?


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.MIMEMulti part import MIMEMultipart
MAIL_SERVER = 'your_server.co m'
MAIL_SUBJECT = "Python.SMT P email test"
MAIL_TO = 'y********@exam ple.com'
MAIL_FROM = "Python.SMT P 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 = """\
<title>Sample HTML File</title>

<h1>Can you see this?</h1>
<p>This is a short paragraph.</p>

msg.attach(MIME Text(html, 'html'))
# print msg.as_string()
# Send the message via our own SMTP server, but don't include the
# envelope header.
smtp = smtplib.SMTP(MA IL_SERVER)
smtp.sendmail(M AIL_FROM, [MAIL_TO], msg.as_string() )
# 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="===== ==========16694 50343=="
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

<title>Sample HTML File</title>

<h1>Can you see this?</h1>
<p>This is a short paragraph.</p>


--=============== 1669450343==--

Sep 28 '06 #1
3 2518
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 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 = """\
attachment = MIMEText(html, 'html')
attachment['Content-Disposition'] = 'attachment; filename="sampl e.html"'
msg.attach(atta chment)
# 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 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

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() statement is almost identical to the example for the module at cpan.org. In fact, I tried replacing my use with the example and got the same result. Can anyone tell me why I'm getting this warning, even though I'm getting the correct output?
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 other views.. and the queries are getting slower and slower. This morning, I'm working on something like this: select <some columns>, "calculatedcolumn" = (select top 1 crap from stuff where
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 into a module, the Control Names and the Fields do not work as the module does not recognise them. Is there a way around this, or do I just live with the repetition of code in the form's module? dixie
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 defaults Linker Error: Undefined symbol isc_detach_database in module TEST.C ..... ..... If I set the option not to generate the underscore (function prefixed with "_") I get the following errors. Interbase 6.0 APIs resolved
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?
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
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.. #include <python2.4/Python.h> void SomeStuff::someMethod (void) { std::cout << "ErrVisitor" << std::endl; PyObject *pName, *pModule, *pDict, *pFunc;
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. import getpass, poplib, email M = poplib.POP3('mail.blah.com') M.user('username') M.pass_('password')
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 successfully, but I do not receive any email. When I execute this code to send email, it gives the following error - 'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\\mscorlib.dll',...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.