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?
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..
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With