Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.