This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/common/bin/email-via-mailgun-smtp

72 lines
1.8 KiB
Plaintext
Raw Normal View History

2019-12-31 16:55:45 -06:00
#!/usr/bin/env bash
err() {
2019-12-31 17:03:22 -06:00
errpre=""
errpost=""
if test -t 1; then
ncolors=$(tput colors)
2020-04-20 10:38:19 -05:00
if test -n "$ncolors" && test "$ncolors" -ge 8; then
2019-12-31 17:03:22 -06:00
errpre="$(tput setaf 1)"
errpost="$(tput setaf 7)"
fi
fi
2020-04-20 10:38:19 -05:00
>&2 echo "${errpre}ERROR: $*${errpost}"; usage; exit 1
2019-12-31 16:55:45 -06:00
}
warn() {
2019-12-31 17:03:22 -06:00
pre=""
post=""
if test -t 1; then
ncolors=$(tput colors)
2020-04-20 10:38:19 -05:00
if test -n "$ncolors" && test "$ncolors" -ge 8; then
2019-12-31 17:03:22 -06:00
pre="$(tput setaf 3)"
post="$(tput setaf 7)"
fi
fi
2020-04-20 10:38:19 -05:00
>&2 echo "${pre}WARNING: $*${post}"
2019-12-31 16:55:45 -06:00
}
usage() { >&2 cat <<USAGEDOC
usage:
email-via-mailgun-smtp [recipient] [subject] [username] [password] <<< 'Hello, world!'
email-via-mailgun-smtp will read all the input from stdin and use the contents as the body of the email.
recipient will default to DEFAULT_MAILGUN_SMTP_RECIPIENT if not set
username will default to DEFAULT_MAILGUN_SMTP_USERNAME if not set
password will default to DEFAULT_MAILGUN_SMTP_PASSWORD if not set
subject will default to DEFAULT_MAILGUN_SMTP_SUBJECT if not set
USAGEDOC
}
recipient="${1:-$DEFAULT_MAILGUN_SMTP_RECIPIENT}"; shift
[[ -z $recipient ]] && err 'No recipient provided.'
subject="${1:-$DEFAULT_MAILGUN_SMTP_SUBJECT}"; shift
[[ -z $subject ]] && warn 'No subject provided. Leaving blank.'
username="${1:-$DEFAULT_MAILGUN_SMTP_USERNAME}"; shift
[[ -z $username ]] && err 'No username provided.'
password="${1:-$DEFAULT_MAILGUN_SMTP_PASSWORD}"; shift
[[ -z $password ]] && err 'No password provided.'
warn "Reading email body from stdin..."
body=""
while read -r line; do
body="${body}\n${line}"
done
2019-12-31 17:03:22 -06:00
[[ -z $body ]] && err 'Body was blank.'
2019-12-31 16:55:45 -06:00
echo "Recipient: $recipient"
2019-12-31 17:03:22 -06:00
>&2 echo "Finished reading body. Sending email..."
2019-12-31 16:55:45 -06:00
swaks --auth \
--server smtp.mailgun.org \
--au "$username" \
--ap "$password" \
--to "$recipient" \
--h-Subject: "$subject" \
2023-07-17 11:23:37 -05:00
--body "$body"