Python To Send Emails Via Outlook Express

ian at kirbyfooty.com ian at kirbyfooty.com
Mon Dec 20 02:41:38 EST 2004


Hi Keith
Thanks for your reply.

I am aware of the smtplib module and it works very well! (refer script
below)

The problem is that I have a developed a freeware application called
Kirby Alarm And Task Scheduler (see www.kirbyfooty.com).
The program can pop up a note, run a program, play a sound, or send an
email at whatever intervals the user wants..
When it comes to sending emails the user has the option of sending them
via smtp, or via there email client (eg outlook express). I prefer the
send method as this makes setting up the email parameters a lot easier
for the user.
As the program is used by over 16,000 people around the world I don't
want to complicate things by asking them to enter the mail server
properties.

Because Python is so powerful I want to develop a suite of applications
in Python that Kirby Alarm can run.
Things like FTP, Backup, Speech etc would be excellent


There has to be a way for Python to send emails via Outlook Express....

Kind regards
Ian

PS Here is the working script for sending emails via SMTP..

-----------------------------------------------------------------------------------------------------------------

# Import smtplib for the actual sending function
import os
import sys
import smtplib
import mimetypes
from email.Encoders import encode_base64
from email.MIMEAudio import MIMEAudio
from email.MIMEBase import MIMEBase
from email.MIMEImage import MIMEImage
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText


FROM = 'i... at kirbyfooty.com'
TO = 'i... at cgbs.com.au;i... at yahoo.com'
SUBJECT = 'This is the subject'
MSGBODY = 'This the body of the message '
ATTACHSTR =
'c:/ian.txt;c:/c55/footytip/2003finalresults.txt;c:/snap.jpg'
MAILSERVER = 'insert mail server'
port = 25
username = 'insert username'
password = 'insert password'

# trim the strings of any leading or trailing spaces
FROM = FROM.strip()
TO = TO.strip()
SUBJECT = SUBJECT.strip()
MSGBODY = MSGBODY.strip()
ATTACHSTR = ATTACHSTR.strip()
MAILSERVER = MAILSERVER.strip()
username = username.strip()
password = password.strip()

# function to attach files
def getAttachment(path, filename):
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
fp = open(path, 'rb')
if maintype == 'text':
attach = MIMEText(fp.read(),_subtype=subtype)
elif maintype == 'message':
attach = email.message_from_file(fp)
elif maintype == 'image':
attach = MIMEImage(fp.read(),_subtype=subtype)
elif maintype == 'audio':
attach = MIMEAudio(fp.read(),_subtype=subtype)
else:
print maintype, subtype
attach = MIMEBase(maintype, subtype)
attach.set_payload(fp.read())
encode_base64(attach)
fp.close
attach.add_header('Content-Disposition', 'attachment',
filename=filename)
return attach

#Connect to server
print 'Connecting to mail server ', MAILSERVER
try:
s = smtplib.SMTP(MAILSERVER,port)
#s.set_debuglevel(1)
except:
print 'ERROR: Unable to connect to mail server', MAILSERVER
sys.exit(1)

#login to server
if password <> '':
print 'Logging into mail erver'
try:
s.login(username,password)
except:
print 'ERROR: Unable to login to mail server', MAILSERVER
print 'Please recheck your password'
sys.exit(1)

# get list of email addresses to send to
ToList = TO.split(';')
print 'Sending email to ', ToList

# set up email parameters
msg = MIMEMultipart()
msg['From'] = FROM
msg['To'] = TO
msg['Subject'] = SUBJECT
msg.attach(MIMEText(MSGBODY))


# get list of file attachments
AttachList = ATTACHSTR.split(';')
for file in AttachList:
try:
attach = getAttachment(file,os.path.basename(file))
msg.attach(attach)
except:
print 'Error attaching ',file
pass


# send email
s.sendmail(FROM, ToList, msg.as_string())
s.quit()
s.close()

print 'done'
-----------------------------------------------------------------------------------------




More information about the Python-list mailing list