Skip to content

Instantly share code, notes, and snippets.

@schubev
Last active June 18, 2017 22:41
Show Gist options
  • Save schubev/19cade15c0fef86862abbd80c5b23ce8 to your computer and use it in GitHub Desktop.
Save schubev/19cade15c0fef86862abbd80c5b23ce8 to your computer and use it in GitHub Desktop.
Wrapper for cron tasks to send e-mails with PGP/MIME encryption
from=$USER@$(hostname -f)
to=$MAILTO
out=$(mktemp)
err=$(mktemp)
started=$(date)
"$@" > $out 2> $err
statuscode=$?
ended=$(date)
if [ -s $out -o -s $err ]
then
{
cat <<END
MIME-Version: 1.0
From: $from
To: $to
Content-Type: multipart/encrypted;
boundary=rootpart;
protocol="application/pgp-encrypted"
--rootpart
Content-Type: application/pgp-encrypted
Content-Description: PGP/MIME Version identification
Version: 1
--rootpart
Content-type: application/octet-stream; name="encrypted.asc"
Content-Description: OpenPGP encrypted message
Content-Disposition: inline; filename="encrypted.asc"
END
{
cat <<END
MIME-Version: 1.0
From: $from
To: $to
Subject: Cron job output
Content-Type: multipart/mixed; boundary=payloadpart
--payloadpart
Content-Type: text/plain; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: base64
END
base64 -w 76 <<END
There was some output from the following cron command:
$@
Started: $started
Ended: $ended
Returned: $statuscode
Run as: $USER
Run in: $(pwd)
END
if [[ -s $out ]]
then
cat <<END
--payloadpart
Content-Type: text/plain; charset="UTF-8"
Content-Disposition: attachment; filename="stdout"
Content-Transfer-Encoding: base64
END
base64 -w 76 < $out
fi
if [[ -s $err ]]
then
cat <<END
--payloadpart
Content-Type: text/plain; charset="UTF-8"
Content-Disposition: attachment; filename="stderr"
Content-Transfer-Encoding: base64
END
base64 -w 76 < $err
fi
cat <<END
--payloadpart--
END
} | gpg --trust-model=always --batch --encrypt --armor --recipient $to
cat <<END
--rootpart--
END
} | sendmail -- $to
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment