Skip to content

Instantly share code, notes, and snippets.

@petrzpav
Created March 6, 2018 21:49
Show Gist options
  • Save petrzpav/a266ef52b3b9ba20e619e2cfe5b629ed to your computer and use it in GitHub Desktop.
Save petrzpav/a266ef52b3b9ba20e619e2cfe5b629ed to your computer and use it in GitHub Desktop.
Bash email function
#!/bin/bash
function email {
local ecode
GLOBAL_CONTENT=${1:-"[Missing message content]"}
GLOBAL_SUBJECT=${2:-"$SCRIPT_NAME Message"}
GLOBAL_TOADDR=${3:-"info@abc.cz"}
GLOBAL_FROMADDR=${4:-"noreply@abc.cz"}
[[ "$GLOBAL_TOADDR" =~ ^$EMAILS_REGEX$ ]] \
|| exception "Invalid TO address $GLOBAL_TOADDR"
[[ "$GLOBAL_FROMADDR" =~ ^$EMAIL_REGEX$ ]] \
|| exception "Invalid FROM address $GLOBAL_FROMADDR"
# shellcheck disable=2086
echo -e "$GLOBAL_CONTENT" | base64 | mail \
-a "Content-Transfer-Encoding: base64" \
-a "Content-Disposition: inline" \
-a "Content-Type: text/plain; charset=UTF-8" \
-aFrom:"$GLOBAL_FROMADDR" \
-s "=?utf-8?B?$(base64 --wrap=0 <<< "$GLOBAL_SUBJECT")?=" \
$GLOBAL_TOADDR
ecode=$?
log "to $GLOBAL_TOADDR (mail exit code $ecode)" "EMAIL"
[[ $ecode != 0 && -n "$3" ]] \
&& email "Failed to send e-mail TO: $GLOBAL_TOADDR" "E-mail $SCRIPT_NAME ERROR"
[[ $ecode == 0 ]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment