Create and display an email

A

Adam Endicott

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?
 
W

Will McGugan

Adam said:
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
 
A

Adam Endicott

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 = '(e-mail address removed)'
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top