forked from mk-fg/fgtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/mail
executable file
·51 lines (45 loc) · 1.49 KB
/mail
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
#!/bin/bash
set -e
usage() {
bin=$(basename $0)
echo >&2 "Usage: $(basename "$0") [opts] rcpt [rcpt ...] < message"
echo >&2 " opts: [-s subject] [-a 'header: value' [-a ...]] [-f mail-from@host]"
echo >&2
echo >&2 "Generated message headers:"
echo >&2 " $header_from"
echo >&2 " $header_date"
echo >&2 " Subject: [exactly as per -s option]"
echo >&2 " [any headers exactly as per -a values]"
echo >&2 " To: [all rcpt args, comma/space-separated]"
echo >&2
echo >&2 "Replacement for crappy sendmail wrappers that do"
echo >&2 " too much stuff but can't/won't allow adding any random headers."
exit ${1:-0}
}
user=$(id -un)
header_from="From: ${user} (${user})"
header_date="Date: $(date --rfc-822)"
letter=( "$header_from" "$header_date" )
rcpt=()
from=()
opt_chk=true
while [[ -n "$1" ]]; do
case "$1" in
-s) shift; letter+=( "Subject: $1" ); shift ;;
-a) shift; letter+=( "$1" ); shift ;;
-f) shift; from=( -f "$1" ); shift ;;
--) opt_chk=; shift ;;
-h|--help) usage ;;
-*) [[ -z "$opt_chk" ]] || { echo >&2 "ERROR: unknown option - $1"; exit 1; } ;;
*) rcpt+=( "$1" ); shift ;;
esac
done
ifs_=$IFS; IFS=", "
letter+=( "To: ${rcpt[*]}" )
IFS=$ifs_; unset ifs_
[[ ${#rcpt[@]} -gt 0 ]] || usage 1
sendmail=/usr/bin/sendmail
[[ -e "$sendmail" ]] || sendmail=/usr/sbin/sendmail
[[ -e "$sendmail" ]] || { echo >&2 "ERROR: sendmail binary not found in /usr/{s,}bin"; exit 1; }
{ for line in "${letter[@]}"; do echo "${line}"; done; echo; cat; } |
exec "$sendmail" "${from[@]}" "${rcpt[@]}"