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

Create and display an email

P: n/a
I've got a wxPython based windows GUI application that takes some input
and creates a PDF file output. At the end, I need to create an email
message with this pdf file attached ready for the user to just hit
"send". I don't want to actually send the email automatically, I just
want to pop up a message in the default mail program that's ready to go
(partially because the person might not be online when the email is
created, this way they could just put it in their Outlook outbox).

I can't figure out how to do this. I've tried using
webbrowser.open('mailto:...'), which works, except that I can't add an
attachment that way (at least I haven't been successful). And I don't
know any other way to open up a message in the default mail program.

There has to be something obvious that I'm missing here. Any
suggestions?

Sep 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Adam Endicott wrote:
I've got a wxPython based windows GUI application that takes some input
and creates a PDF file output. At the end, I need to create an email
message with this pdf file attached ready for the user to just hit
"send". I don't want to actually send the email automatically, I just
want to pop up a message in the default mail program that's ready to go
(partially because the person might not be online when the email is
created, this way they could just put it in their Outlook outbox).

I can't figure out how to do this. I've tried using
webbrowser.open('mailto:...'), which works, except that I can't add an
attachment that way (at least I haven't been successful). And I don't
know any other way to open up a message in the default mail program.

There has to be something obvious that I'm missing here. Any
suggestions?


I dont think there is anything in the standard library to help you here.
Windows has an api called 'MAPI' which does this sort of thing. There
may be bindings to it somewhere, or you could create your own with
ctypes. I sure you will find something now you know what to google for..

Will McGugan
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")
Sep 15 '05 #2

P: n/a
Thanks for the MAPI suggestion. I did a bit more googling, and combined
a few things, along with some trial and error to come up with something
that works. Here it is for posterity:

================================
import win32com.client
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application.11")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = 'A subject line'
newMail.Body = 'Body, wonderful body'
newMail.To = 's*******@example.com'
filename = r'c:\test.txt'
newMail.Attachments.Add(filename)
newMail.Display()
================================

Everything I saw just had "Outlook.Application" on line 3, but that was
giving me an error:

pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)

Looking in the makepy generated file for Outlook, I saw:

# This CoClass is known by the name 'Outlook.Application.11'

referring to the Application class. So I tried that and it worked. I
assume that the number will need to be changed for different versions
of Outlook, or perhaps is only necessary for 11 (11+?).

The other thing to note is that this still gave me an error:

pywintypes.com_error: (-2147024770, 'The specified module could not be
found.', None, None)

if Outlook was not actually open and running. Also, I believe the
filename for the attachment needs to be an absolute path. But with
Outlook open, this code works and displays an email message with the
specified Subject, Body, addressee, and attachment.

Hopefully somebody else can find this useful.

Sep 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.