How to open the user's preferred mail application on Linux?
Asked Answered
A

3

6

I wrote a simple native GUI script with python-gtk. Now I want to give the user a button to send an email with an attachment.

The script runs on Linux desktops. Is there a way to open the user's preferred mail application and attach a file?

Amann answered 1/12, 2014 at 20:44 Comment(1)
Related: #27837076Amann
E
6

The linux command to call would be xdg-email, part of the xdg-utils package, which is on most linux desktops (at least by default on arch, debian, ubuntu).

xdg-email is a "command line tool for sending mail using the user's preferred e-mail composer".

provided they've set up their default applications properly, it will open their default mail client. You can pass it arguments to fill in various mail fields (to, cc, subject, body etc.), as well as file names of the files to attach.

From your python script you could call it using os.system() or the subprocess module..

Ecosphere answered 9/12, 2014 at 7:24 Comment(8)
Thank you! It is even available on old openSUSE 11.4.Amann
Adding attachments does not work with xdg-email if the client is thunderbird. Very sad. I guess most users use thunderbird. Welcome to linux desktop plumbing: bugs.launchpad.net/ubuntu/+source/simple-scan/+bug/515386Amann
Until the above bug in xdg-email exists, I use this for thunderbird: kb.mozillazine.org/Command_line_arguments_%28Thunderbird%29Amann
Next bug: I am calling thunderbird from the commandline. But attaching several files don't work for me. Only the first file gets attached: thunderbird -compose "attachment='/etc/mtab',attachment='/etc/fstab'"Amann
@Amann That should be thunderbird -compose "attachment='/etc/mstab,/etc/fstab'"Greatniece
@ThijsvanDien I tried you version of the command line, but it does not work. Does it work for you?Amann
@Amann I don't have the right environment to test in; I merely noticed this example on the KB page you linked.Greatniece
@ThijsvanDien your solution contained a typo: "mstab". This file does not exist. This works: thunderbird -compose "attachment='/etc/mtab,/etc/fstab'"Amann
E
4

You can leverage the webbrowser module to open a URL.
You can also leverage the mailto protocol to have the webbrowser open the system default mail client if available.

Here is a simple example:

import webbrowser
webbrowser.open("mailto:[email protected]?subject=Hello World")

Caveat, no support for attachments. The mailto protocol doesn't offer support for attachments. Some clients support (according to google) the nonstandard attribute attachment=PATH. But I haven't been able to actually confirm this.

There are ways for various email clients to open an email compose window with an attachment but this differs between each client. Also I don't know of any standard way to determine what mail program is set as default.

For more information you can also check wikipedia

Enidenigma answered 5/12, 2014 at 22:5 Comment(3)
Maybe the webbrowser instance could be somehow created with an associative array, containing the attachment files? Congrat to the first triage steward badge! :-)Fiscus
The mailto protocol itself has no official support for file attachments so it's unlikely that passing an array of any sort will make a difference. (and I was second)Enidenigma
Then the mail (with its attachments) could be created with some third api call, maybe.Fiscus
R
-2

this is how you set user agent

  settings = webkit.WebSettings()
  settings.set_property('user-agent', 'iPad')
  webview.set_settings(settings)

and for attaching images, take a look at this script to get an idea

http://pygtk.org/pygtk2tutorial/examples/images.py

this isn't definite but I hope it helps.

Rubberneck answered 1/12, 2014 at 20:54 Comment(1)
I don't understand your answer. It looks like you modify the HTTP User Agent. I want to open the native mail user agent: Thunderbird, evolution ...Amann

© 2022 - 2024 — McMap. All rights reserved.