[Tutor] Re: Mail....
Sean Steeg
steegness at hotmail.com
Mon Oct 4 18:28:20 CEST 2004
-----Original Message-----
Message: 1
Date: Mon, 4 Oct 2004 06:32:06 -0700 (PDT)
From: Ali Polatel <alipolatel at yahoo.com>
Subject: [Tutor] Mail...
To: Tutor at python.org
Message-ID: <20041004133206.94661.qmail at web61001.mail.yahoo.com>
Content-Type: text/plain; charset="us-ascii"
What is the easiest way to send e-mails using Python?
Can you show me a sample not complicated script?
Regards
-----------------------------------------------------
Ali,
I wrote something not long ago that might help. The script itself isn't
TOO complicated, and calling it from other modules was designed to be as
simple as I could think of making it. It supports attachments too,
which is pretty keen (if I do say so myself)
It's also available at
http://www.uselesspython.com/users/ssteeg/arch_Tech.html
Sean
--------------------
import smtplib, mimetypes, string, email, os
from email.Encoders import encode_base64
from email.MIMEText import MIMEText
from email.MIMEAudio import MIMEAudio
from email.MIMEMultipart import MIMEMultipart
from email.MIMEImage import MIMEImage
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
class SimpleMessage:
def __init__(self):
self._files = []
def AttachFile(self, fullname):
if not fullname in self._files:
self._files.append(fullname)
return 1
else: return 0
def DetachFile(self, fullname):
if fullname in self._files:
return self._files.pop(self._files.index(fullname))
else: return 0
def To(self, s):
"""If multiple recipients, use a [list] or separate with
commas."""
if type(s) == type([]):
self._to = s
else:
self._to = s.split(',')
def Sender(self, s):
self._sender = s
def Subject(self, s):
self._subject = s
def Body(self, s):
self._body = s
def _prepletter(self):
#super(SimpleMessage, self).__init__()
#Body and headers.
outer = MIMEMultipart()
outer['Subject'] = self._subject
outer['To'] = string.join(self._to, ',')
outer['From'] = self._sender
outer.preamble = 'MIME Message'
outer.epilogue = ''
body = MIMEText(self._body)
outer.attach(body)
for eachfile in self._files:
ctype, encoding = mimetypes.guess_type(eachfile)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
f = file(eachfile, 'rb')
if maintype == 'text':
inner = MIMEText(f.read(), _subtype=subtype)
elif maintype == 'message':
inner = email.message_from_file(f)
elif maintype == 'image':
inner = MIMEImage(f.read(), _subtype=subtype)
elif maintype == 'audio':
inner = MIMEAudio(f.read(), _subtype=subtype)
else:
inner = MIMEBase(maintype, subtype)
inner.set_payload(f.read())
encode_base64(inner)
f.close()
inner.add_header('Content-Disposition', 'attachment',
filename=os.path.basename(eachfile))
outer.attach(inner)
return outer.as_string()
def Send(self, host='localhost', u='', p=''):
if not self._to:
print "No recipients specified."
return 0
sendtext = self._prepletter()
server = smtplib.SMTP(host)
if u:
server.login(u, p)
try:
server.sendmail(self._sender, string.join(self._to, ','),
sendtext)
server.quit()
except:
return 0
return 1
------------------------
More information about the Tutor
mailing list