invoke user's standard mail client

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • luc.saffre@gmail.com

    #1

    invoke user's standard mail client

    Hello,

    the simplest way to launch the user's standard mail client from a
    Python program is by creating a mailto: URL and launching the
    webbrowser:

    def mailto_url(to=N one,subject=Non e,body=None,cc= None):
    """
    encodes the content as a mailto link as described on
    http://www.faqs.org/rfcs/rfc2368.html
    Examples partly taken from

    """
    url = "mailto:" + urllib.quote(to .strip(),"@,")
    sep = "?"
    if cc:
    url+= sep + "cc=" + urllib.quote(cc ,"@,")
    sep = "&"
    if subject:
    url+= sep + "subject=" + urllib.quote(su bject,"")
    sep = "&"
    if body:
    # Also note that line breaks in the body of a message MUST be
    # encoded with "%0D%0A". (RFC 2368)
    body="\r\n".joi n(body.splitlin es())
    url+= sep + "body=" + urllib.quote(bo dy,"")
    sep = "&"
    return url

    import webbrowser
    url = mailto_url(...)
    webbrowser.open (url,new=1)

    (Excerpt from http://svn.berlios.de/wsvn/lino/trun...ile&rev=0&sc=0)

    But this method is limited: you cannot specify a file to be attached
    to the mail. And I guess that there would be problems if the body text
    is too complex.

    Does somebody know about a better method?
    It should be possible at least on Windows, since Acrobat Reader is
    able to do it.

    Thanks in advance for any hints!
    Luc Saffre

  • Tim Golden

    #2
    Re: invoke user's standard mail client

    luc.saffre@gmai l.com wrote:
    the simplest way to launch the user's standard mail client from a
    Python program is by creating a mailto: URL and launching the
    webbrowser:
    [... snip code ...]
    But this method is limited: you cannot specify a file to be attached
    to the mail. And I guess that there would be problems if the body text
    is too complex.
    Does somebody know about a better method?
    It should be possible at least on Windows, since Acrobat Reader is
    able to do it.
    I'm going to stick my neck out and say: I doubt
    if there's one recognised, approved method. That
    would require every email client to have a way
    of accepting a command which said "Open up a new
    email and put this, this, and this into that field,
    that space, and that attachment." The only thing
    I can think of which comes close is the mailto:
    protocol you refer to, but according to its RFC

    http://www.faqs.org/rfcs/rfc2368.html

    the only fields allowed are headers and the special
    case of "body" which, as you point out, is hardly
    intended for embedded attachments.

    I would imagine that Acrobat must special case
    known email clients and probably won't work for
    some obscure client. Thunderbird, for example,
    seems (haven't tried it) to allow for an attachment:

    The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps.


    Doubtless Outlook has some equivalent mechanism. After
    that, you're down to looking at docs for Eudora, Pine,
    etc.

    TJG

    Comment

    • Cameron Laird

      #3
      Re: invoke user's standard mail client

      In article <1178266064.922 925.125760@y80g 2000hsf.googleg roups.com>,
      luc.saffre@gmai l.com <luc.saffre@gma il.comwrote:
      >Hello,
      >
      >the simplest way to launch the user's standard mail client from a
      >Python program is by creating a mailto: URL and launching the
      >webbrowser:
      >
      >def mailto_url(to=N one,subject=Non e,body=None,cc= None):
      """
      encodes the content as a mailto link as described on
      http://www.faqs.org/rfcs/rfc2368.html
      Examples partly taken from

      """
      url = "mailto:" + urllib.quote(to .strip(),"@,")
      sep = "?"
      if cc:
      url+= sep + "cc=" + urllib.quote(cc ,"@,")
      sep = "&"
      if subject:
      url+= sep + "subject=" + urllib.quote(su bject,"")
      sep = "&"
      if body:
      # Also note that line breaks in the body of a message MUST be
      # encoded with "%0D%0A". (RFC 2368)
      body="\r\n".joi n(body.splitlin es())
      url+= sep + "body=" + urllib.quote(bo dy,"")
      sep = "&"
      return url
      >
      >import webbrowser
      >url = mailto_url(...)
      >webbrowser.ope n(url,new=1)
      >
      >(Excerpt from
      >http://svn.berlios.de/wsvn/lino/trun...ile&rev=0&sc=0)
      >
      >But this method is limited: you cannot specify a file to be attached
      >to the mail. And I guess that there would be problems if the body text
      >is too complex.
      >
      >Does somebody know about a better method?
      >It should be possible at least on Windows, since Acrobat Reader is
      >able to do it.

      Comment

      • Gabriel Genellina

        #4
        Re: invoke user's standard mail client

        En Fri, 04 May 2007 05:07:44 -0300, luc.saffre@gmai l.com
        <luc.saffre@gma il.comescribió:
        the simplest way to launch the user's standard mail client from a
        Python program is by creating a mailto: URL and launching the
        webbrowser:
        But this method is limited: you cannot specify a file to be attached
        to the mail. And I guess that there would be problems if the body text
        is too complex.
        Does somebody know about a better method?
        It should be possible at least on Windows, since Acrobat Reader is
        able to do it.
        On Windows you can use MAPI.

        --
        Gabriel Genellina

        Comment

        • Stefan Sonnenberg-Carstens

          #5
          Re: invoke user's standard mail client

          Gabriel Genellina schrieb:
          En Fri, 04 May 2007 05:07:44 -0300, luc.saffre@gmai l.com
          <luc.saffre@gma il.comescribió:
          >
          >
          >the simplest way to launch the user's standard mail client from a
          >Python program is by creating a mailto: URL and launching the
          >webbrowser:
          >But this method is limited: you cannot specify a file to be attached
          >to the mail. And I guess that there would be problems if the body text
          >is too complex.
          >Does somebody know about a better method?
          >It should be possible at least on Windows, since Acrobat Reader is
          >able to do it.
          >>
          >
          On Windows you can use MAPI.
          >
          >
          import win32api
          win32api.ShellE xecute(0,'open' ,'mailto:',None ,None,0)

          Comment

          • Stefan Sonnenberg-Carstens

            #6
            Re: invoke user's standard mail client

            Stefan Sonnenberg-Carstens schrieb:
            Gabriel Genellina schrieb:
            >
            >En Fri, 04 May 2007 05:07:44 -0300, luc.saffre@gmai l.com
            ><luc.saffre@gm ail.comescribió :
            >>
            >>
            >>
            >>the simplest way to launch the user's standard mail client from a
            >>Python program is by creating a mailto: URL and launching the
            >>webbrowser:
            >>But this method is limited: you cannot specify a file to be attached
            >>to the mail. And I guess that there would be problems if the body text
            >>is too complex.
            >>Does somebody know about a better method?
            >>It should be possible at least on Windows, since Acrobat Reader is
            >>able to do it.
            >>>
            >>>
            >On Windows you can use MAPI.
            >>
            >>
            >>
            import win32api
            win32api.ShellE xecute(0,'open' ,'mailto:',None ,None,0)
            >
            For completeness

            import win32api
            win32api.ShellE xecute(0,'open' ,'mailto: guido@python.or g',None,None,0)

            Comment

            • luc.saffre@gmail.com

              #7
              Re: invoke user's standard mail client

              On May 6, 9:50 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
              On Windows you can use MAPI.
              But how? I could not find any starting point.

              I found examples about sending mail directly, which gives me the
              impression that MAPI is just Microsoft's version of SMTP. This is not
              what I need. I need the user's client to start, so that the user may
              edit the message and decide herself whether she clicks on the Send
              button to really send it.

              Luc

              Comment

              • luc.saffre@gmail.com

                #8
                Re: invoke user's standard mail client

                On May 6, 10:08 am, Stefan Sonnenberg-Carstens
                <stefan.sonnenb ...@pythonmeist er.comwrote:
                Stefan Sonnenberg-Carstens schrieb:
                >
                Gabriel Genellina schrieb:
                >
                En Fri, 04 May 2007 05:07:44 -0300, luc.saf...@gmai l.com
                <luc.saf...@gma il.comescribió:
                >
                >the simplest way to launch the user's standard mail client from a
                >Python program is by creating a mailto: URL and launching the
                >webbrowser:
                >But this method is limited: you cannot specify a file to be attached
                >to the mail. And I guess that there would be problems if the body text
                >is too complex.
                >Does somebody know about a better method?
                >It should be possible at least on Windows, since Acrobat Reader is
                >able to do it.
                >
                On Windows you can use MAPI.
                >
                import win32api
                win32api.ShellE xecute(0,'open' ,'mailto:',None ,None,0)
                >
                For completeness
                >
                import win32api
                win32api.ShellE xecute(0,'open' ,'mailto: g...@python.org ',None,None,0)
                That's equivalent to what the webbrowser module does in my example.
                Except that your program won't work on Unix.
                Luc

                Comment

                • luc.saffre@gmail.com

                  #9
                  Re: invoke user's standard mail client

                  On May 4, 11:42 am, Tim Golden <m...@timgolden .me.ukwrote:
                  I'm going to stick my neck out and say: I doubt
                  if there's one recognised, approved method. That
                  would require every email client to have a way
                  of accepting a command which said "Open up a new
                  email and put this, this, and this into that field,
                  that space, and that attachment."
                  Tim, I agree, but I hope that we are both wrong.
                  I would imagine that Acrobat must special case
                  known email clients and probably won't work for
                  some obscure client. Thunderbird, for example,
                  seems (haven't tried it) to allow for an attachment:
                  >
                  The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps.

                  >
                  Doubtless Outlook has some equivalent mechanism. After
                  that, you're down to looking at docs for Eudora, Pine,
                  etc.
                  That's what I plan to do if you are right. At least for Thunderbird
                  and Outlook...

                  Luc

                  Comment

                  • Gabriel Genellina

                    #10
                    Re: invoke user's standard mail client

                    En Mon, 07 May 2007 01:52:18 -0300, luc.saffre@gmai l.com
                    <luc.saffre@gma il.comescribió:
                    On May 6, 9:50 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
                    >On Windows you can use MAPI.
                    But how? I could not find any starting point.
                    Get the pywin32 package (Python for Windows extensions) from sourceforge,
                    install it, and look into the win32comext\map i\demos directory.
                    I found examples about sending mail directly, which gives me the
                    impression that MAPI is just Microsoft's version of SMTP. This is not
                    what I need. I need the user's client to start, so that the user may
                    edit the message and decide herself whether she clicks on the Send
                    button to really send it.
                    No, it should launch the email client (Outlook Express by example) and let
                    the user confirm it. I think there were some flags to suppress the GUI or
                    the confirmation, but they're not honored anymore, I presume. At least
                    Eudora warns the user on such attempts.

                    --
                    Gabriel Genellina

                    Comment

                    • Paul Boddie

                      #11
                      Re: invoke user's standard mail client

                      On 4 Mai, 18:54, cla...@lairds.u s (Cameron Laird) wrote:
                      .
                      Portland <URL:http://ct.enews.eweek. com/rd/cts?d=186-6281-53-799-798304-697089-0-0-0-1>
                      is the best standardization of this problem we have under Linux.
                      >
                      I'll address Windows in a subsequent follow-up.
                      Portland [1] provides scripts (xdg-open, xdg-email...) which overlap
                      with the functionality provided by the desktop module:



                      The desktop module should even work with Windows as well, but it seems
                      that xdg-email has the edge in terms of providing the inquirer's
                      desired support for composing e-mail messages (on Free Software
                      desktops, anyway).

                      Paul

                      [1] http://portland.freedesktop.org/wiki/

                      Comment

                      • Cameron Laird

                        #12
                        Re: invoke user's standard mail client

                        In article <u25sg4-irp.ln1@lairds. us>, Cameron Laird <claird@lairds. uswrote:
                        >In article <1178266064.922 925.125760@y80g 2000hsf.googleg roups.com>,
                        >luc.saffre@gma il.com <luc.saffre@gma il.comwrote:
                        >>Hello,
                        >>
                        >>the simplest way to launch the user's standard mail client from a
                        >>Python program is by creating a mailto: URL and launching the
                        >>webbrowser:
                        >>
                        >>def mailto_url(to=N one,subject=Non e,body=None,cc= None):
                        > """
                        > encodes the content as a mailto link as described on
                        > http://www.faqs.org/rfcs/rfc2368.html
                        > Examples partly taken from
                        > http://selfhtml.teamone.de/html/verweise/email.htm
                        > """
                        > url = "mailto:" + urllib.quote(to .strip(),"@,")
                        > sep = "?"
                        > if cc:
                        > url+= sep + "cc=" + urllib.quote(cc ,"@,")
                        > sep = "&"
                        > if subject:
                        > url+= sep + "subject=" + urllib.quote(su bject,"")
                        > sep = "&"
                        > if body:
                        > # Also note that line breaks in the body of a message MUST be
                        > # encoded with "%0D%0A". (RFC 2368)
                        > body="\r\n".joi n(body.splitlin es())
                        > url+= sep + "body=" + urllib.quote(bo dy,"")
                        > sep = "&"
                        > return url
                        >>
                        >>import webbrowser
                        >>url = mailto_url(...)
                        >>webbrowser.op en(url,new=1)
                        >>
                        >>(Excerpt from
                        >>http://svn.berlios.de/wsvn/lino/trun...ile&rev=0&sc=0)
                        >>
                        >>But this method is limited: you cannot specify a file to be attached
                        >>to the mail. And I guess that there would be problems if the body text
                        >>is too complex.
                        >>
                        >>Does somebody know about a better method?
                        >>It should be possible at least on Windows, since Acrobat Reader is
                        >>able to do it.
                        > .
                        > .
                        > .
                        >Portland <URL:
                        >http://ct.enews.eweek.com/rd/cts?d=1...697089-0-0-0-1 >
                        >is the best standardization of this problem we have under Linux.
                        >
                        >I'll address Windows in a subsequent follow-up.
                        A. Apologies! I'm sorry about the URL above; it was
                        completely wrong. I intended <URL:
                        http://www-128.ibm.com/developerwork...-portland.html >.
                        B. The best approach I know under Windows is to invoke
                        start mailto:$ADDRESS
                        1. That invocation does *not* communicate
                        subject, attachments, ... To do so
                        adequately involves application-specific
                        work, as other follow-ups have mentioned.
                        2. "start" has its own complexities. The
                        best invocation from console-based Python
                        is likely to be
                        start /w "" mailto:$ADDRESS

                        Comment

                        • luc.saffre@gmail.com

                          #13
                          Re: invoke user's standard mail client

                          On May 7, 10:28 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
                          wrote:
                          >
                          Get the pywin32 package (Python for Windows extensions) from sourceforge,
                          install it, and look into the win32comext\map i\demos directory.
                          Thanks for the hint, Gabriel.
                          Wow, that's heavily spiced code! When I invoke mapisend.py I get:

                          Traceback (most recent call last):
                          File "mapisend1. py", line 85, in <module>
                          SendEMAPIMail(S endSubject, SendMessage, SendTo,
                          MAPIProfile=MAP IProfile)
                          File "mapisend1. py", line 23, in SendEMAPIMail
                          mapi.MAPIInitia lize(None)
                          pywintypes.com_ error: (-2147467259, 'Unspecified error', None, None)

                          But what is a MAPI profile? I left this variable blank. Do I need MS
                          Exchange Server to run this demo?

                          Comment

                          • Gabriel Genellina

                            #14
                            Re: invoke user's standard mail client

                            En Mon, 07 May 2007 18:00:06 -0300, luc.saffre@gmai l.com
                            <luc.saffre@gma il.comescribió:
                            On May 7, 10:28 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
                            wrote:
                            >>
                            >Get the pywin32 package (Python for Windows extensions) from
                            >sourceforge,
                            >install it, and look into the win32comext\map i\demos directory.
                            >
                            Thanks for the hint, Gabriel.
                            Wow, that's heavily spiced code! When I invoke mapisend.py I get:
                            >
                            Traceback (most recent call last):
                            File "mapisend1. py", line 85, in <module>
                            SendEMAPIMail(S endSubject, SendMessage, SendTo,
                            MAPIProfile=MAP IProfile)
                            File "mapisend1. py", line 23, in SendEMAPIMail
                            mapi.MAPIInitia lize(None)
                            pywintypes.com_ error: (-2147467259, 'Unspecified error', None, None)
                            >
                            But what is a MAPI profile? I left this variable blank.
                            You can register several profiles (or users, or accounts); leave it blank
                            to use the default profile.
                            Do I need MS
                            Exchange Server to run this demo?
                            No. But this simple example used to work fine for me, but not anymore :( .
                            Perhaps it has to do with my Eudora configuration. I've never used Outlook
                            nor OutlookExpress btw.
                            You may find this thread interesting:


                            --
                            Gabriel Genellina

                            Comment

                            • Leo Kislov

                              #15
                              Re: invoke user's standard mail client

                              On May 7, 2:00 pm, "luc.saf...@gma il.com" <luc.saf...@gma il.com>
                              wrote:
                              On May 7, 10:28 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
                              wrote:
                              >
                              >
                              >
                              Get the pywin32 package (Python for Windows extensions) from sourceforge,
                              install it, and look into the win32comext\map i\demos directory.
                              >
                              Thanks for the hint, Gabriel.
                              Wow, that's heavily spiced code! When I invoke mapisend.py I get:
                              >
                              Traceback (most recent call last):
                              File "mapisend1. py", line 85, in <module>
                              SendEMAPIMail(S endSubject, SendMessage, SendTo,
                              MAPIProfile=MAP IProfile)
                              File "mapisend1. py", line 23, in SendEMAPIMail
                              mapi.MAPIInitia lize(None)
                              pywintypes.com_ error: (-2147467259, 'Unspecified error', None, None)
                              >
                              But what is a MAPI profile?
                              It's an abstraction of incoming and outgoing mail accounts. In UNIX
                              terms it's kind of like running local sendmail that forwards mail to
                              another server and fetchmail that fetches mail from external inboxes,
                              e.g. it's a proxy between you and outgoing/incoming mail server.
                              I left this variable blank. Do I need MS
                              Exchange Server to run this demo?
                              No, but you need an account on some mail server and some email program
                              should create a MAPI profile to represent that account on your local
                              computer. As I understand creation of MAPI profiles is not a common
                              practice among non-Microsoft products, for example my computer with
                              Lotus Notes doesn't have any MAPI profiles.

                              -- Leo

                              Comment

                              Working...