Skip to content

Instantly share code, notes, and snippets.

@rasschaert
Last active March 1, 2019 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasschaert/4c1b8a6a50ba7895dd6f5abf8aa00861 to your computer and use it in GitHub Desktop.
Save rasschaert/4c1b8a6a50ba7895dd6f5abf8aa00861 to your computer and use it in GitHub Desktop.
#!/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="kenny@example.com"
# 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