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?

guettli
  • 25,042
  • 81
  • 346
  • 663
  • Related: http://stackoverflow.com/questions/27836576/adding-several-attachments-to-thunderbird-from-command-line – guettli Jan 08 '15 at 09:18

3 Answers3

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

nik
  • 726
  • 2
  • 12
  • 28
  • Thank you! It is even available on old openSUSE 11.4. – guettli Dec 09 '14 at 07:48
  • 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: https://bugs.launchpad.net/ubuntu/+source/simple-scan/+bug/515386 – guettli Jan 08 '15 at 08:35
  • Until the above bug in xdg-email exists, I use this for thunderbird: http://kb.mozillazine.org/Command_line_arguments_%28Thunderbird%29 – guettli Jan 08 '15 at 08:53
  • 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'"` – guettli Jan 08 '15 at 09:14
  • 1
    @guettli That should be `thunderbird -compose "attachment='/etc/mstab,/etc/fstab'"` – Thijs van Dien Jan 08 '15 at 09:28
  • @ThijsvanDien I tried you version of the command line, but it does not work. Does it work for you? – guettli Jan 08 '15 at 09:51
  • @guettli I don't have the right environment to test in; I merely noticed this example on the KB page you linked. – Thijs van Dien Jan 08 '15 at 10:02
  • @ThijsvanDien your solution contained a typo: "m**s**tab". This file does not exist. This works: `thunderbird -compose "attachment='/etc/mtab,/etc/fstab'"` – guettli Jan 18 '15 at 18:44
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:test@example.com?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

EWit
  • 1,954
  • 13
  • 22
  • 19
  • Maybe the webbrowser instance could be somehow created with an associative array, containing the attachment files? Congrat to the first triage steward badge! :-) – peterh Jan 21 '15 at 22:06
  • 1
    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) – EWit Jan 24 '15 at 12:15
  • Then the mail (with its attachments) could be created with some third api call, maybe. – peterh Jan 24 '15 at 12:17
-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.

unixmiah
  • 3,081
  • 1
  • 12
  • 26
  • 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 ... – guettli Dec 02 '14 at 07:41