invoke user's standard mail client

L

luc.saffre

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/trunk/src/lino/tools/mail.py?op=file&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
 
T

Tim Golden

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
 
C

Cameron Laird

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/trunk/src/lino/tools/mail.py?op=file&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=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.
 
G

Gabriel Genellina

En Fri, 04 May 2007 05:07:44 -0300, (e-mail address removed)
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.
 
S

Stefan Sonnenberg-Carstens

Gabriel said:
En Fri, 04 May 2007 05:07:44 -0300, (e-mail address removed)


On Windows you can use MAPI.
import win32api
win32api.ShellExecute(0,'open','mailto:',None,None,0)
 
S

Stefan Sonnenberg-Carstens

Stefan said:
import win32api
win32api.ShellExecute(0,'open','mailto:',None,None,0)
For completeness

import win32api
win32api.ShellExecute(0,'open','mailto: (e-mail address removed)',None,None,0)
 
L

luc.saffre

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
 
L

luc.saffre

For completeness

import win32api
win32api.ShellExecute(0,'open','mailto: (e-mail address removed)',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
 
L

luc.saffre

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
 
G

Gabriel Genellina

En Mon, 07 May 2007 01:52:18 -0300, (e-mail address removed)
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.
 
P

Paul Boddie

.
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/
 
C

Cameron Laird

.
.
.
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.

A. Apologies! I'm sorry about the URL above; it was
completely wrong. I intended <URL:
http://www-128.ibm.com/developerworks/linux/library/l-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
 
L

luc.saffre

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

Gabriel Genellina

En Mon, 07 May 2007 18:00:06 -0300, (e-mail address removed)
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/python-win32/2005-November/003985.html
 
L

Leo Kislov

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
 

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

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top