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

invoke user's standard mail client

P: n/a
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=None,subject=None,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(subject,"")
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".join(body.splitlines())
url+= sep + "body=" + urllib.quote(body,"")
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

May 4 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
lu********@gmail.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:

http://www.mozilla.org/docs/command-line-args.html

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

TJG
May 4 '07 #2

P: n/a
In article <11**********************@y80g2000hsf.googlegroups .com>,
lu********@gmail.com <lu********@gmail.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=None,subject=None,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(subject,"")
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".join(body.splitlines())
url+= sep + "body=" + urllib.quote(body,"")
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.
May 4 '07 #3

P: n/a
En Fri, 04 May 2007 05:07:44 -0300, lu********@gmail.com
<lu********@gmail.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
May 6 '07 #4

P: n/a
Gabriel Genellina schrieb:
En Fri, 04 May 2007 05:07:44 -0300, lu********@gmail.com
<lu********@gmail.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.ShellExecute(0,'open','mailto:',None,None ,0)
May 6 '07 #5

P: n/a
Stefan Sonnenberg-Carstens schrieb:
Gabriel Genellina schrieb:
>En Fri, 04 May 2007 05:07:44 -0300, lu********@gmail.com
<lu********@gmail.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.ShellExecute(0,'open','mailto:',None,None ,0)
For completeness

import win32api
win32api.ShellExecute(0,'open','mailto: gu***@python.org',None,None,0)

May 6 '07 #6

P: n/a
On May 6, 9:50 am, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
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

May 7 '07 #7

P: n/a
On May 6, 10:08 am, Stefan Sonnenberg-Carstens
<stefan.sonnenb...@pythonmeister.comwrote:
Stefan Sonnenberg-Carstens schrieb:
Gabriel Genellina schrieb:
En Fri, 04 May 2007 05:07:44 -0300, luc.saf...@gmail.com
<luc.saf...@gmail.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.ShellExecute(0,'open','mailto:',None,None ,0)

For completeness

import win32api
win32api.ShellExecute(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

May 7 '07 #8

P: n/a
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:

http://www.mozilla.org/docs/command-line-args.html

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

May 7 '07 #9

P: n/a
En Mon, 07 May 2007 01:52:18 -0300, lu********@gmail.com
<lu********@gmail.comescribió:
On May 6, 9:50 am, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
>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\mapi\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

May 7 '07 #10

P: n/a
On 4 Mai, 18:54, cla...@lairds.us (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:

http://www.python.org/pypi/desktop

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/

May 7 '07 #11

P: n/a
In article <u2************@lairds.us>, Cameron Laird <cl****@lairds.uswrote:
>In article <11**********************@y80g2000hsf.googlegroups .com>,
lu********@gmail.com <lu********@gmail.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=None,subject=None,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(subject,"")
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".join(body.splitlines())
url+= sep + "body=" + urllib.quote(body,"")
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.
.
.
.
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
May 7 '07 #12

P: n/a
On May 7, 10:28 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>
Get the pywin32 package (Python for Windows extensions) from sourceforge,
install it, and look into the win32comext\mapi\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(SendSubject, SendMessage, SendTo,
MAPIProfile=MAPIProfile)
File "mapisend1.py", line 23, in SendEMAPIMail
mapi.MAPIInitialize(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?

May 7 '07 #13

P: n/a
En Mon, 07 May 2007 18:00:06 -0300, lu********@gmail.com
<lu********@gmail.comescribió:
On May 7, 10:28 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>>
Get the pywin32 package (Python for Windows extensions) from
sourceforge,
install it, and look into the win32comext\mapi\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(SendSubject, SendMessage, SendTo,
MAPIProfile=MAPIProfile)
File "mapisend1.py", line 23, in SendEMAPIMail
mapi.MAPIInitialize(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:
http://mail.python.org/pipermail/pyt...er/003985.html

--
Gabriel Genellina

May 8 '07 #14

P: n/a
On May 7, 2:00 pm, "luc.saf...@gmail.com" <luc.saf...@gmail.com>
wrote:
On May 7, 10:28 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
Get the pywin32 package (Python for Windows extensions) from sourceforge,
install it, and look into the win32comext\mapi\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(SendSubject, SendMessage, SendTo,
MAPIProfile=MAPIProfile)
File "mapisend1.py", line 23, in SendEMAPIMail
mapi.MAPIInitialize(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

May 8 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.