2
#---------------------------------------------
5
# Utility script to open the users favorite email program, using the
6
# RFC 2368 mailto: URI spec
8
# Refer to the usage() function below for usage.
10
# Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
11
# Copyright 2006, Jeremy White <jwhite@codeweavers.com>
15
# Permission is hereby granted, free of charge, to any person obtaining a
16
# copy of this software and associated documentation files (the "Software"),
17
# to deal in the Software without restriction, including without limitation
18
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
19
# and/or sell copies of the Software, and to permit persons to whom the
20
# Software is furnished to do so, subject to the following conditions:
22
# The above copyright notice and this permission notice shall be included
23
# in all copies or substantial portions of the Software.
25
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31
# OTHER DEALINGS IN THE SOFTWARE.
33
#---------------------------------------------
40
xdg-email - command line tool for sending mail using the user's preferred
45
xdg-email [--utf8] [--cc address] [--bcc address] [--subject text] [--body text
46
] [--attach file] [ mailto-uri | address(es) ]
48
xdg-email { --help | --manual | --version }
52
xdg-email opens the user's preferred e-mail composer in order to send a mail to
53
address(es) or mailto-uri. RFC2368 defines mailto: URIs. xdg-email limits
54
support to, cc, subject and body fields in mailto-uri, all other fields are
55
silently ignored. address(es) must follow the syntax of RFC822. Multiple
56
addresses may be provided as separate arguments.
58
All information provided on the command line is used to prefill corresponding
59
fields in the user's e-mail composer. The user will have the opportunity to
60
change any of this information before actually sending the e-mail.
62
xdg-email is for use inside a desktop session only. It is not recommended to
63
use xdg-email as root.
65
See http://portland.freedesktop.org/EmailConfig for information on how the user
66
can change the e-mail composer that is used.
71
Indicates that all command line options that follow are in utf8. Without
72
this option, command line options are expected to be encoded according to
73
locale. If the locale already specifies utf8 this option has no effect.
74
This option does not affect mailto URIs that are passed on the command
77
Specify a recipient to be copied on the e-mail.
79
Specify a recipient to be blindly copied on the e-mail.
81
Specify a subject for the e-mail.
83
Specify a body for the e-mail. Since the user will be able to make changes
84
before actually sending the e-mail, this can be used to provide the user
85
with a template for the e-mail. text may contain linebreaks.
88
Specify an attachment for the e-mail. file must point to an existing file.
90
Some e-mail applications require the file to remain present after xdg-email
94
Show command synopsis.
98
Show the xdg-utils version information.
100
Environment Variables
102
xdg-email honours the following environment variables:
104
XDG_UTILS_DEBUG_LEVEL
105
Setting this environment variable to a non-zero numerical value makes
106
xdg-email do more verbose reporting on stderr. Setting a higher value
107
increases the verbosity.
111
An exit code of 0 indicates success while a non-zero exit code indicates
112
failure. The following failure codes can be returned:
115
Error in command line syntax.
117
One of the files passed on the command line did not exist.
119
A required tool could not be found.
123
No permission to read one of the files passed on the command line.
127
Visit http://portland.freedesktop.org/EmailConfig for information how to
128
configure xdg-email to use the email client of your choice.
132
xdg-email 'Jeremy White <jwhite@example.com>'
134
xdg-email --attach /tmp/logo.png \
135
--subject 'Logo contest' \
136
--body 'Attached you find the logo for the contest.' \
139
xdg-email --subject 'Your password is about to expire' \
140
'jwhite@example.com' 'bastian@example.com' 'whipple@example.com'
148
xdg-email - command line tool for sending mail using the user's preferred
153
xdg-email [--utf8] [--cc address] [--bcc address] [--subject text] [--body text
154
] [--attach file] [ mailto-uri | address(es) ]
156
xdg-email { --help | --manual | --version }
163
#----------------------------------------------------------------------------
164
# Common utility functions included in all XDG wrapper scripts
165
#----------------------------------------------------------------------------
169
[ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
170
[ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
175
#-------------------------------------------------------------
176
# Exit script on successfully completing the desired operation
180
if [ $# -gt 0 ]; then
189
#-----------------------------------------
190
# Exit script on malformed arguments, not enough arguments
191
# or missing required option.
192
# prints usage information
194
exit_failure_syntax()
196
if [ $# -gt 0 ]; then
197
echo "xdg-email: $@" >&2
198
echo "Try 'xdg-email --help' for more information." >&2
201
echo "Use 'man xdg-email' or 'xdg-email --manual' for additional info."
207
#-------------------------------------------------------------
208
# Exit script on missing file specified on command line
210
exit_failure_file_missing()
212
if [ $# -gt 0 ]; then
213
echo "xdg-email: $@" >&2
219
#-------------------------------------------------------------
220
# Exit script on failure to locate necessary tool applications
222
exit_failure_operation_impossible()
224
if [ $# -gt 0 ]; then
225
echo "xdg-email: $@" >&2
231
#-------------------------------------------------------------
232
# Exit script on failure returned by a tool application
234
exit_failure_operation_failed()
236
if [ $# -gt 0 ]; then
237
echo "xdg-email: $@" >&2
243
#------------------------------------------------------------
244
# Exit script on insufficient permission to read a specified file
246
exit_failure_file_permission_read()
248
if [ $# -gt 0 ]; then
249
echo "xdg-email: $@" >&2
255
#------------------------------------------------------------
256
# Exit script on insufficient permission to write a specified file
258
exit_failure_file_permission_write()
260
if [ $# -gt 0 ]; then
261
echo "xdg-email: $@" >&2
269
if [ ! -e "$1" ]; then
270
exit_failure_file_missing "file '$1' does not exist"
272
if [ ! -r "$1" ]; then
273
exit_failure_file_permission_read "no permission to read file '$1'"
277
check_vendor_prefix()
280
[ -n "$file_label" ] || file_label="filename"
288
echo "xdg-email: $file_label '$file' does not have a proper vendor prefix" >&2
289
echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
290
echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
291
echo "Use --novendor to override or 'xdg-email --manual' for additional info." >&2
297
# if the file exists, check if it is writeable
298
# if it does not exists, check if we are allowed to write on the directory
300
if [ ! -w "$1" ]; then
301
exit_failure_file_permission_write "no permission to write to file '$1'"
305
if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
306
exit_failure_file_permission_write "no permission to create file '$1'"
311
#----------------------------------------
312
# Checks for shared commands, e.g. --help
314
check_common_commands()
316
while [ $# -gt 0 ] ; do
323
echo "Use 'man xdg-email' or 'xdg-email --manual' for additional info."
333
echo "xdg-email 1.0.2"
340
check_common_commands "$@"
342
[ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
343
if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
345
xdg_redirect_output=" > /dev/null 2> /dev/null"
347
# All output to stderr
348
xdg_redirect_output=" >&2"
351
#--------------------------------------
352
# Checks for known desktop environments
353
# set variable DE to the desktop environments name, lowercase
357
if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
358
elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
359
elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
360
elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
364
#----------------------------------------------------------------------------
365
# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
366
# It also always returns 1 in KDE 3.4 and earlier
367
# Simply return 0 in such case
369
kfmclient_fix_exit_code()
371
version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep KDE`
372
major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
373
minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
374
release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
375
test "$major" -gt 3 && return $1
376
test "$minor" -gt 5 && return $1
377
test "$release" -gt 4 && return $1
383
local THUNDERBIRD MAILTO NEWMAILTO TO CC BCC SUBJECT BODY ATTACH
385
MAILTO=$(echo "$2" | sed 's/^mailto://')
386
echo "$MAILTO" | grep -qs "^?"
387
if [ "$?" = "0" ] ; then
388
MAILTO=$(echo "$MAILTO" | sed 's/^?//')
390
MAILTO=$(echo "$MAILTO" | sed 's/^/to=/' | sed 's/?/\&/')
393
MAILTO=$(echo "$MAILTO" | sed 's/&/\n/g')
394
TO=$(echo "$MAILTO" | grep '^to=' | sed 's/^to=//' | awk '{ printf "%s,",$0 }')
395
CC=$(echo "$MAILTO" | grep '^cc=' | sed 's/^cc=//' | awk '{ printf "%s,",$0 }')
396
BCC=$(echo "$MAILTO" | grep '^bcc=' | sed 's/^bcc=//' | awk '{ printf "%s,",$0 }')
397
SUBJECT=$(echo "$MAILTO" | grep '^subject=' | tail -n 1)
398
BODY=$(echo "$MAILTO" | grep '^body=' | tail -n 1)
399
ATTACH=$(echo "$MAILTO" | sed 's/^attach=/\n\nfile:\/\//g' | awk '/^file:/ { printf "%s,",$0 }')
401
if [ -z "$TO" ] ; then
406
if [ -n "$CC" ] ; then
407
NEWMAILTO="${NEWMAILTO},cc='$CC'"
409
if [ -n "$BCC" ] ; then
410
NEWMAILTO="${NEWMAILTO},bcc='$BCC'"
412
if [ -n "$SUBJECT" ] ; then
413
NEWMAILTO="${NEWMAILTO},$SUBJECT"
415
if [ -n "$BODY" ] ; then
416
NEWMAILTO="${NEWMAILTO},$BODY"
419
if [ -n "$ATTACH" ] ; then
420
NEWMAILTO="${NEWMAILTO},attachment='${ATTACH}'"
423
NEWMAILTO=$(echo "$NEWMAILTO" | sed 's/^,//')
424
DEBUG 1 "Running $THUNDERBIRD -compose \"$NEWMAILTO\""
425
"$THUNDERBIRD" -compose "$NEWMAILTO"
426
if [ $? -eq 0 ]; then
429
exit_failure_operation_failed
436
client=`kreadconfig --file emaildefaults --group PROFILE_Default --key EmailClient | cut -d ' ' -f 1`
437
echo $client | grep thunderbird > /dev/null 2>&1
438
if [ $? -eq 0 ] ; then
439
run_thunderbird "$client" "$1"
442
if [ -f /etc/SuSE-release ] ; then
443
# Workaround for SUSE 10.0
444
[ -z "$client" ] && client="kmail"
445
if ! which "$client" > /dev/null 2> /dev/null; then
446
DEBUG 3 "KDE has $client configured as email client which isn't installed"
447
if which gnome-open > /dev/null 2> /dev/null && which evolution > /dev/null 2> /dev/null; then
448
DEBUG 3 "Try gnome-open instead"
453
DEBUG 1 "Running kmailservice \"$1\""
454
if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
455
KMAILSERVICE=`kde4-config --locate kmailservice --path exe 2>/dev/null`
457
KMAILSERVICE=`which kmailservice 2>/dev/null`
459
# KDE uses locale's encoding when decoding the URI, so set it to UTF-8
460
LC_ALL=C.UTF-8 $KMAILSERVICE "$1"
461
kfmclient_fix_exit_code $?
463
if [ $? -eq 0 ]; then
466
exit_failure_operation_failed
473
client=`gconftool-2 --get /desktop/gnome/url-handlers/mailto/command | cut -d ' ' -f 1` || ""
474
echo $client | grep thunderbird > /dev/null 2>&1
475
if [ $? -eq 0 ] ; then
476
run_thunderbird "$client" "$1"
479
if gvfs-open --help 2>/dev/null 1>&2; then
480
DEBUG 1 "Running gvfs-open \"$1\""
483
DEBUG 1 "Running gnome-open \"$1\""
487
if [ $? -eq 0 ]; then
490
exit_failure_operation_failed
497
DEBUG 1 "Running exo-open \"$1\""
500
if [ $? -eq 0 ]; then
503
exit_failure_operation_failed
510
for browser in $BROWSER; do
511
if [ x"$browser" != x"" ]; then
513
browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
514
if [ $? -ne 0 ]; then browser_with_arg=$browser;
517
if [ x"$browser_with_arg" = x"$browser" ]; then "$browser" "$1";
518
else $browser_with_arg;
521
if [ $? -eq 0 ]; then exit_success;
526
exit_failure_operation_impossible "no method available for opening '$1'"
531
result=$(echo "$1" | $utf8 | awk '
533
for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
538
if ( linenr++ != 1 ) {
541
for ( i=1; i<=length ($0); ++i ) {
542
c = substr ($0, i, 1)
543
if ( ord [c] > 127 ) {
544
e = e "%" sprintf("%02X", ord [c])
545
} else if ( c ~ /[@a-zA-Z0-9.-\\/]/ ) {
548
e = e "%" sprintf("%02X", ord [c])
561
while [ $# -gt 0 ] ; do
571
if [ -z "$1" ] ; then
572
exit_failure_syntax "email address argument missing for --to"
575
options="${options}to=${result}&"
580
if [ -z "$1" ] ; then
581
exit_failure_syntax "email address argument missing for --cc"
584
options="${options}cc=${result}&"
589
if [ -z "$1" ] ; then
590
exit_failure_syntax "email address argument missing for --bcc"
593
options="${options}bcc=${result}&"
598
if [ -z "$1" ] ; then
599
exit_failure_syntax "text argument missing for --subject option"
602
options="${options}subject=${result}&"
607
if [ -z "$1" ] ; then
608
exit_failure_syntax "text argument missing for --body option"
611
options="${options}body=${result}&"
616
if [ -z "$1" ] ; then
617
exit_failure_syntax "file argument missing for --attach option"
619
check_input_file "$1"
620
file=`readlink -f "$1"` # Normalize path
621
if [ -z "$file" -o ! -f "$file" ] ; then
622
exit_failure_file_missing "file '$1' does not exist"
626
options="${options}attach=${result}&"
631
exit_failure_syntax "unexpected option '$parm'"
640
if [ -z "${mailto}" ] ; then
641
mailto="mailto:"${result}"?"
643
options="${options}to=${result}&"
648
exit_failure_syntax "unexpected argument '$parm'"
653
if [ -z "${mailto}" ] ; then
654
# TO address is optional
660
mailto="${mailto}${options}"
664
mailto="${mailto}&${options}"
668
mailto="${mailto}?${options}"
672
# Strip trailing ? and &
673
mailto=`echo "${mailto}"| sed 's/[?&]$//'`
676
[ x"${mailto}" != x"" ] || exit_failure_syntax
678
if which xdg-email-hook.sh > /dev/null 2> /dev/null; then
679
xdg-email-hook.sh "${mailto}"
680
if [ $? -eq 0 ]; then
683
exit_failure_operation_failed
689
if [ x"$DE" = x"" ]; then
690
# if BROWSER variable is not set, check some well known browsers instead
691
if [ x"$BROWSER" = x"" ]; then
692
BROWSER=firefox:mozilla:netscape
703
open_gnome "${mailto}"
707
open_xfce "${mailto}"
711
open_generic "${mailto}"
715
exit_failure_operation_impossible "no method available for opening '${mailto}'"