By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

How to add an attachment with smtplib module

P: n/a
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.

Thanks.

--
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wed, 28 Apr 2004 20:22:32 GMT, se**@sands.beach.net (Sean Berry)
declaimed the following in comp.lang.python:
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
email.

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.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #2

P: n/a
Sean Berry <se**@sands.beach.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.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()
msg.attach(MIMEText(file("text.txt").read())
msg.attach(MIMEImage(file("image.png").read())

And then send it:

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

Read email package documentation for more details.
Jul 18 '05 #3

P: n/a
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.

Thanks.

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.

regards
Steve
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.