-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailme.sh
More file actions
executable file
·75 lines (69 loc) · 1.66 KB
/
mailme.sh
File metadata and controls
executable file
·75 lines (69 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#! /usr/bin/env bash
command -v mailx >/dev/null 2>&1 || { echo >&2 "Required command mailx is not installed."; exit 1; }
if ! grep mta $HOME/.mailrc 2>/dev/null | grep smtp 2>/dev/null 1>&2; then
echo "SMTP must be configured" >&2
exit 1
fi
CONF=$HOME/.config/mailme.conf
if [ ! -e $CONF ]; then
echo "Can not find $CONF" >&2
echo "Example content:" >&2
echo "ME=mymail@mydomain.org" >&2
exit 1
fi
source $CONF
if [ -z ${ME+x} ]; then
echo "Variable ME must be set in $CONF"
exit 1
fi
tmp="$(mktemp -d)"
cleanup() {
if [ -d "$tmp" ]; then
rm -rf "$tmp"
fi
}
cleanup2() {
sleep 1
cleanup
}
trap cleanup2 SIGINT
trap cleanup SIGTERM
mailme() {
if tty -s; then
if [ $# -eq 0 ]; then
echo -e "No arguments specified.\nUsage:\n $(basename $0) <file|directory>\n ... | $(basename $0) <file_name>" >&2
return 1
fi
subject=
if [ $# -eq 1 ]; then
subject="$*"
else
subject="$# files"
fi
cmd="mailx -s '$subject'"
if [ $# -gt 0 ]; then
while [[ "$1" ]]; do
if [ ! -e "$1" ]; then
echo "$1: No such file or directory" >&2
return 1
elif [ -d "$1" ]; then
echo "compressing $1"
zipped="${tmp}/$(basename "$1").zip"
(cd "$1" && zip -1 -r -q - .) | cat > "$zipped"
cmd="$cmd -a '$zipped'"
else
cmd="$cmd -a '$1'"
fi
shift
done
cmd="$cmd $ME"
echo "sending $subject to $ME"
saveIFS="$IFS"; IFS=''; date | eval $cmd; IFS="$saveIFS"
fi
else
echo "sending stdin as a body to $ME"
cat | mailx -s 'stdin' $ME
fi
}
mailme "$@"
cleanup