Add email-at
This commit is contained in:
parent
aeb79cfb4f
commit
d6ae54bb68
|
@ -0,0 +1,114 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -o errexit
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
|
||||
declare SENDER="$LOGNAME@$(hostname)"
|
||||
declare RECIPIENT="$SENDER"
|
||||
declare CONTENT_PATH=""
|
||||
|
||||
function show_help ()
|
||||
{
|
||||
declare -i exitcode=$1
|
||||
cat >&2 <<EOF
|
||||
usage: $(basename $0) [OPTION...] SUBJECT TIMESPEC
|
||||
|
||||
Compose an email for sending at a later time. The text of the email
|
||||
is a brief message automatically generated, or you can use '-f' to
|
||||
specify the text.
|
||||
|
||||
OPTIONS
|
||||
-s ADDR Sender, defaults to '$SENDER'.
|
||||
-r ADDR Recipient, defaults to '$RECIPIENT'.
|
||||
-f PATH File containing the text of the mail.
|
||||
Use "-f -" to type the text when prompted.
|
||||
-v Verbosity. Repeat for more verbosity.
|
||||
-h This help.
|
||||
|
||||
TIMESPEC
|
||||
A time or datetime as understood by at(1).
|
||||
|
||||
Examples
|
||||
\$ $(basename $0) "Buy milk" now + 12 hours
|
||||
\$ $(basename $0) "Sell butter" 09:30 2030-04-01
|
||||
EOF
|
||||
exit $exitcode
|
||||
}
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
declare -i VERBOSITY=0
|
||||
while getopts ":hs:r:f:v" opt
|
||||
do
|
||||
case $opt in
|
||||
s ) SENDER="$OPTARG" ;;
|
||||
r ) RECIPIENT="$OPTARG" ;;
|
||||
f ) CONTENT_PATH="$OPTARG" ;;
|
||||
v ) ((++VERBOSITY)) ;;
|
||||
* ) show_help 0 ;;
|
||||
esac
|
||||
done
|
||||
shift $(($OPTIND - 1))
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
function squawk ()
|
||||
{
|
||||
local -i NVOL=$1; shift
|
||||
if ((NVOL <= VERBOSITY)); then
|
||||
echo $(basename $0) "$@"
|
||||
fi
|
||||
}
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
squawk 1 "started, sender '$SENDER', recipient '$RECIPIENT', CONTENT_PATH '$CONTENT_PATH'."
|
||||
|
||||
if (($# < 2)); then
|
||||
show_help 1
|
||||
fi
|
||||
|
||||
declare -r SUBJECT="$1"; shift
|
||||
|
||||
declare -r TMPFILE=$(mktemp /var/tmp/email-at.XXXX)
|
||||
squawk 1 "TMPFILE is $TMPFILE"
|
||||
|
||||
if [[ -z "$CONTENT_PATH" ]]; then
|
||||
cat > "$TMPFILE" <<EOF
|
||||
Generated by $(basename $0) on $(date "+%F %a %T %Z").
|
||||
EOF
|
||||
elif [[ "-" == "$CONTENT_PATH" ]]; then
|
||||
echo "Type your message here, ending with ctrl-d."
|
||||
cat >$TMPFILE
|
||||
# Is it empty?
|
||||
declare -i TMPSZ=$(stat --format='%s' $TMPFILE)
|
||||
squawk 1 "Size of TMPFILE is $TMPSZ bytes"
|
||||
if ((0 == TMPSZ)); then
|
||||
echo >&2 "No content, aborting"
|
||||
rm "$TMPFILE"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
squawk 1 "Looking for contents in '$CONTENT_PATH'..."
|
||||
if ! [[ -f "$CONTENT_PATH" && -r "$CONTENT_PATH" ]]; then
|
||||
echo >&2 "'$CONTENT_PATH': not found or unreadable."
|
||||
exit 1
|
||||
fi
|
||||
squawk 1 "Found '$CONTENT_PATH'."
|
||||
cp "$CONTENT_PATH" "$TMPFILE"
|
||||
fi
|
||||
|
||||
# Append signature.
|
||||
printf -- "-- \n%s\n" $(basename $0) >> "$TMPFILE"
|
||||
|
||||
squawk 0 "content copied to $TMPFILE"
|
||||
squawk 1 "Sub $SUBJECT; Rec $RECIPIENT; time $@"
|
||||
at $@ -f <(cat <<EOF0
|
||||
bash -s <<EOF1
|
||||
mutt -e "unset record" -e "my_hdr From: $SENDER" -s "$SUBJECT" "$RECIPIENT" < "$TMPFILE"
|
||||
rm "$TMPFILE"
|
||||
EOF1
|
||||
EOF0
|
||||
)
|
||||
|
||||
squawk 1 "done."
|
||||
|
||||
exit 0
|
Loading…
Reference in New Issue