Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 476
guettli Avatar asked Dec 01 '14 20:12

guettli


2 Answers

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

like image 52
nik Avatar answered Sep 22 '22 12:09

nik


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

like image 25
EWit Avatar answered Sep 22 '22 12:09

EWit