Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:linux-tutorials:send-command-output-via-email [2025/09/08 16:43] – removed - external edit (Unknown date) 127.0.0.1linux:linux-tutorials:send-command-output-via-email [2025/09/08 18:09] (current) Zyzonix
Line 1: Line 1:
 +===== 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:
 +
 +<panel type="primary" icon="glyphicon glyphicon-file" title="/path/to/script">
 +<code bash>
 +#!/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
 +</code>
 +</panel>