Create and display an email

Discussion in 'Python' started by Adam Endicott, Sep 14, 2005.

  1. 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?
    Adam Endicott, Sep 14, 2005
    #1
    1. Advertising

  2. Adam Endicott

    Will McGugan Guest

    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")
    Will McGugan, Sep 15, 2005
    #2
    1. Advertising

  3. 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 = ''
    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.
    Adam Endicott, Sep 15, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Twain

    Display and display model

    Marc Twain, Jan 13, 2004, in forum: Java
    Replies:
    1
    Views:
    377
    Matt Humphrey
    Jan 13, 2004
  2. Replies:
    2
    Views:
    1,111
    Mike Meyer
    Jun 22, 2005
  3. Replies:
    5
    Views:
    113
  4. ecureuil
    Replies:
    0
    Views:
    325
    ecureuil
    May 28, 2006
  5. Replies:
    13
    Views:
    469
    Randy Webb
    May 14, 2006
Loading...

Share This Page