Connecting Tech Pros Worldwide Forums | Help | Site Map

Create and display an email

Adam Endicott
Guest
 
Posts: n/a
#1: Sep 14 '05
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?


Will McGugan
Guest
 
Posts: n/a
#2: Sep 15 '05

re: Create and display an email


Adam Endicott wrote:[color=blue]
> 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?
>[/color]

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")
Adam Endicott
Guest
 
Posts: n/a
#3: Sep 15 '05

re: Create and display an email


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 = 'somebody@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.

Closed Thread