473,594 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to add an attachment with smtplib module

I have the sample script that uses the smtplib module
that is available at python.org.

How can I use this to send an attachment?

Help is greatly appreciated.


Jul 18 '05 #1
3 4679
On Wed, 28 Apr 2004 20:22:32 GMT, se**@sands.beac h.net (Sean Berry)
declaimed the following in comp.lang.pytho n:
I have the sample script that uses the smtplib module
that is available at python.org.

How can I use this to send an attachment?
By brute force... You create the proper MIME headers to identify
the presence of an attachment; you use the proper module(s) to convert
the file of the attachment into an SMTP safe format (Base64?); you copy
the attachment, with proper MIME boundary markers, into the body of the

No doubt there is a simpler, more automated method, but by
knowing what goes into the process, you can get more control over it.

smtplib is merely the low level interface between a sending
client and the receiving smtp daemon. The "msg" argument has to be a
fully formed message with all headers and body.

Check mimetools, mimify, and email modules.

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 18 '05 #2
Sean Berry <se**@sands.bea ch.net> wrote:
I have the sample script that uses the smtplib module
that is available at python.org.

How can I use this to send an attachment?

You don't. smtplib is a transport for an already formatted email
message. You create a message with email package, like this:

from email.MIMEMulti part import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()
msg.attach(MIME Text(file("text .txt").read())
msg.attach(MIME Image(file("ima ge.png").read() )

And then send it:

import smtplib
mailer = smtplib.SMTP()
mailer.connect( )
mailer.sendmail (from_, to, msg.as_string() )

Read email package documentation for more details.
Jul 18 '05 #3
Sean Berry wrote:
I have the sample script that uses the smtplib module
that is available at python.org.

How can I use this to send an attachment?

Help is greatly appreciated.


Take a look at the "email" module, which is the usual way of generating
messages of complex structure. Then, specifically, look at "Creating
email and MIME objects from scratch" at (e.g.)
http://docs.python.org/lib/node501.html - you'll probably have the same
page in your local Python docs.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Stephen Boulet | last post by:
Is it possible to send a file as an attachment using smtplib? -- Stephen If your desktop gets out of control easily, you probably have too much stuff on it that doesn't need to be there. Donna Smallin, "Unclutter Your Home"
by: Copelandia Cyanescens | last post by:
Python Python 2.3.4, Windows, "clean" install, using the example code given in the documentation, I get this... Traceback (most recent call last): File "C:\projects\email\email.py", line 24, in ? server = smtplib.SMTP('mail.xxxxx.xxx') AttributeError: 'module' object has no attribute 'SMTP' I beg to differ, it most certainly does. Right? :)
by: Tim Williams | last post by:
I have a working SMTP client that I need to add TLS capability to, I absolutely need the client to timeout within a specified time, but when I use the sock.timeout() line it freezes the reading of chars from SSLFakeFile used during TLS instead of timing out the client connection. I am working from my own hacked version of smtplib, but can demonstrate the problem using the standard module (Python 2.3.4). Can anyone suggest a...
by: praba kar | last post by:
Dear All, I have doubt regarding mail sending smtplib module. The below code is I used to send a mail. ########################################## import email.Message import email.Utils import mimetypes import os,string
by: Van_Gogh | last post by:
Hi, I am learning how to use the smtplib module, but am having some very early problems, maybe because I don't understand it. So, am I correct that by following the example in the Python: >>> import smtplib >>> server = smtplib.SMTP('localhost') >>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org', """To: jcaesar@example.org
by: krishnakant Mane | last post by:
hello, I am a bit confused. I want to make a program that will take some data from a database and make a string of text. and send it to the respective email id of a person. next I also want to send an attachment of a photo along with the email. I will be required to make this program run on windows xp. can some one guide me as to how I can achieve this? what I will need on windows to send emails? I believe yahoo, gmail, rediff and msn...
by: Roger | last post by:
I am having a problem sending email through smtp.gmail.com using smtplib. Everything works and the mail is sent and received, except quit. The following shows the problem (without bothering to login or do the sendmail): I have not had this problem with other mail servers (but only tried two). A solution for gmail seems to be to replace the server.quit() with server.close(). The difference between the two commands is quit() sends a...
by: Eric E | last post by:
Hello All - I am using python to send an email with a large zip file as an attachment. I successfully sent a 52M attachment. If I try to send a 63M attachment or larger, the message never gets through. I do not get any errors in my python code. I pasted my python code below. from email.MIMEBase import MIMEBase from email.MIMEMultipart import MIMEMultipart from email import Encoders
by: zxo102 | last post by:
Hi, I am trying to use python module smtplib to send my email out on window xp (localhost). import smtplib server = smtplib.SMTP('localhost') but I got the error information as follows: Traceback (most recent call last):
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: 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: 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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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, and deployment—without 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: 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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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: 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: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.