Send output of command via email with sendmail

This guide shows how to configure a script to send it's output via email. Thereby that the MAILTO= setting in crontabs is global, it's not that easy to send an email to a different address from another crontab files. This function/script can be used to send the output of a crontab to a different address:

#!/bin/bash RECIPIENT=user@example.com SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_NAME=$(basename "$0") SENDER=root@$(/bin/hostname) crontab () { echo "MIME-Version: 1.0" echo "Content-Type: text/plain; charset=\"UTF-8\"" echo "From: $SENDER" echo "Subject: <$SENDER> $SCRIPT_DIR/$SCRIPT_NAME " echo "To: $RECIPIENT" # Commands to be run here echo "" echo "Script location: $SCRIPT_DIR/$SCRIPT_NAME" } crontab | sendmail -t $RECIPIENT