![]() |
Python To Send Emails Via Outlook Express
Hi,
I'm a newbie (oh no I can here you say.... another one...) How can I get Python to send emails using the default windows email client (eg outlook express)? I thought I could just do the following import win32com.client s = win32com.client.Dispatch('CDO.Message') s.From = "ian@kirbyfooty.com" s.To = "someone@yahoo.com" s.Subject = "The subject" s.Send .... but nothing happens. What am I doing wrong? Does anyone have some sample code to share with me please? Thank you! Ian Cook (freeware author of Kirby Alarm And Task Scheduler www.kirbyfooty.com) |
Re: Python To Send Emails Via Outlook Express
>>>>> "ian" == ian <ian@kirbyfooty.com> writes:
> import win32com.client > s = win32com.client.Dispatch('CDO.Message') > s.From = "ian@kirbyfooty.com" > s.To = "someone@yahoo.com" > s.Subject = "The subject" > s.Send > ... but nothing happens. > What am I doing wrong? Does anyone have some sample code to share with > me please? Try s.Send(). Ganesan |
Re: Python To Send Emails Via Outlook Express
Hi Ganesan
I tried changing s.Send to s.Send(). It now comes up with an exception error.. The details are below. Ian Traceback (most recent call last): File "C:\Python23\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" , line 307, in RunScript debugger.run(codeObject, __main__.__dict__, start_stepping=0) File "C:\Python23\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run _GetCurrentDebugger().run(cmd, globals,locals, start_stepping) File "C:\Python23\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 595, in run exec cmd in globals, locals File "D:\MyPython\emailtest.py", line 7, in ? s.Send() File "C:\Python23\lib\site-packages\win32com\gen_py\CD000000-8B95-11D1-82DB-00C04FB1625Dx0x1x0.py", line 686, in Send return self._oleobj_.InvokeTypes(158, LCID, 1, (24, 0), (),) com_error: (-2147352567, 'Exception occurred.', (0, None, 'The server rejected one or more recipient addresses. The server response was: 554 <iwcook@yahoo.com>: Relay access denied\r\n', None, 0, -2147220977), None) |
Re: Python To Send Emails Via Outlook Express
<ian@kirbyfooty.com> wrote:
> I tried changing s.Send to s.Send(). It now comes up with an exception > error.. > com_error: (-2147352567, 'Exception occurred.', (0, None, 'The server > rejected one or more recipient addresses. The server response was: 554 > <iwcook@yahoo.com>: Relay access denied\r\n', None, 0, -2147220977), > None) sure looks like you managed to talk to the mail program, but your mail server thinks you're trying to send faked mails. your local mail admins can probably help you sort this one out. (googling for "Relay access denied" could also help) </F> |
Re: Python To Send Emails Via Outlook Express
Thanks Fredrik,
That was my first impression too. But all I want to do is use Python to instruct Outlook Express to send an email. That way the user would not have to do any setting up etc of the mail server properties etc and Outlook Express will magage all the connection side of things. I have seen other python scripts that will talk to Excel etc but so far (despite a lot of searching) I cannot see how Python can talk to Outlook Express. I know I can do this in Clarion by accessing MapiSendEmail but because I'm new to Python I don't know how to do it in Python. I'm really impressed with the power of Python. It seems just about anything you can think of is there already so I know th eanswer is out there somewhere! Can this be done using Python? Does anyone have a sample script please? Pretty please?? <grin> Ian |
Re: Python To Send Emails Via Outlook Express
ian@kirbyfooty.com wrote:
> Hi Ganesan > I tried changing s.Send to s.Send(). It now comes up with an exception > error.. > > The details are below. Looks like the COM part works, but sending mail has an error from the SMTP host. But, slightly off topic, FYI, Python can send email directly with the email and snmplib modules. -- -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ Keith Dart <kdart@kdart.com> public key: ID: F3D288E4 ================================================== =================== |
Re: Python To Send Emails Via Outlook Express
<ian@kirbyfooty.com> wrote:
> But all I want to do is use Python to instruct Outlook Express to send > an email. and you succeeded -- the error message you saw came from the mail server, not outlook itself. your problem is that the server didn't like the mail you sent; checking the server configuration (or just the server logs) can help you figure out why. have you tried different From/To settings, btw? </F> |
Re: Python To Send Emails Via Outlook Express
ian@kirbyfooty.com wrote:
> Thanks Fredrik, > That was my first impression too. > > But all I want to do is use Python to instruct Outlook Express to send > an email. Which you did! From the look of the traceback. But your mailserver is configured in such a way that you cannot send mail from your machine using those email adresse, or you don't log on with the correct credentials. -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science |
Re: Python To Send Emails Via Outlook Express
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...@kirbyfooty.com' TO = 'i...@cgbs.com.au;i...@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' ----------------------------------------------------------------------------------------- |
Re: Python To Send Emails Via Outlook Express
Hi Fredrik,
Thank you for the suggestion. I tried different from/to settings and guess what? The mail came thru. The script is now.. import win32com.client s = win32com.client.Dispatch('CDO.Message') s.From = "ian@cgbs.com.au" (was "ian@kirbyfooty.com") s.To = "ian@kirbyfooty.com" (was "someone@yahoo.com") s.Subject = "The subject" s.Send() My problem is thought, the message is still not being sent via Outlook Express. What am I missing? Thanks again for your help so far!! Kind regards Ian |
| All times are GMT. The time now is 09:19 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.