Skip to content

Instantly share code, notes, and snippets.

@craigphicks
Last active July 13, 2018 23:43
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 craigphicks/60b679b819d129bc60d4136c5664888c to your computer and use it in GitHub Desktop.
Save craigphicks/60b679b819d129bc60d4136c5664888c to your computer and use it in GitHub Desktop.
Replace "sendmail" with (a link to) the script, and forward all system mails via free Mailgun acct to your chosen external address
#!/bin/bash
# Documentation can be found on either of following blogs (with mirrored content):
# https://pindertek.net/2018/07/04/sendmail-free-mailgun/
# https://craigphicks.github.io/2018/07/04/sendmail-free-mailgun/
# NOTE: root permission required to write to /var/log.
# Choose another log location if that will not be suitable.
Logfile=/var/log/sendmail-dummy.log
if [[ $EUID -ne 0 ]]; then
Logfile=${HOME}/sendmail-dummy.log
fi
Tmpf=$(mktemp -t sendmail-dummy-ml-XXXXXX.txt)
TmpCurlLog=$(mktemp -t sendmail-dummy-re-XXXXXX.txt)
trap 'rm -f ${Tmpf} ${TmpCurlLog}' 0
Date=$(date +%F-%T)
echo "[$Date] Caller: $(caller)" >>${Tmpf}
echo "[$Date] Caller: $0" >>${Tmpf}
echo "[$Date] Args: ${@}" >>${Tmpf}
echo "[$Date] Content:" >>${Tmpf}
while read line ; do
echo $line >>${Tmpf}
done
echo "" >>${Tmpf}
# If you have signed up free mailgun account then login and go to url:
# https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-via-api
# to see the approriate values for the following variables
MailgunDomain="()"
Key="()"
FromAddr="()"
ToAddr="()"
curl -s --user "api:${Key}" "https://api.mailgun.net/v3/${Key}/messages" \
-F https://api.mailgun.net/v3/${MailgunDomain}/messages \
-F from=" <$FromAddr>" \
-F to="${ToAddr}" \
-F subject='Notification' \
-F text="<${Tmpf}" > ${TmpCurlLog}
rc=$?
echo "----------------------------------------" >> ${Logfile}
cat ${Tmpf} >> ${Logfile}
echo "----------------------------------------" >> ${Logfile}
echo "[$Date] curl result = $rc" >> ${Logfile}
cat ${TmpCurlLog} >> ${Logfile}
echo "" >> ${Logfile}
echo "++++++++++++++++++++++++++++++++++++++++" >> ${Logfile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment