Last active
March 1, 2019 12:48
-
-
Save rasschaert/4c1b8a6a50ba7895dd6f5abf8aa00861 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script wraps around ssmtp. It makes a bit easier for me to send emails from cron. | |
# How to use this script: | |
# send-email.sh --subject="hello world" --body_file="/path/to/file.txt" --email="[email protected]" | |
# Set PATH | |
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
function fail() { | |
echo "$@" 1>&2 | |
exit 1 | |
} | |
# Parse arguments | |
# https://unix.stackexchange.com/a/204927/466 | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--email=*) | |
email="${1#*=}" | |
;; | |
--subject=*) | |
subject="${1#*=}" | |
;; | |
--body_file=*) | |
body_file="${1#*=}" | |
[[ ! -f "$body_file" ]] && fail \ | |
"Error: file \"${body_file}\" does not exist." | |
;; | |
*) | |
invalid="${1#*=}" | |
fail "Error: \"${invalid}\" is an invalid argument." | |
;; | |
esac | |
shift | |
done | |
# Combine the arguments into a message that makes sense to ssmtp and send it. | |
printf "Subject: %s\n\n%s\n" "$subject" "$(cat $body_file)" | ssmtp "${email}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment