Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have had some trouble with getting cron to send emails. My ISP requires that the "From:" field match the email address the email is being sent from, otherwise the email is rejected. As cron hardcodes the "From:" field to "root (Cron Daemon)", these emails are not being sent.

I have set up msmtp and can send emails with no problem from the command line. In fact I have wrapped some of my cron jobs in a script that sends the email itself. This works fine, but I would like a more elegant solution.

I originally used the package msmtp-mta, which just symlinks /usr/lib/sendmail to /usr/bin/msmtp so that cron will send emails using msmtp. Since that didn't work, I removed the package and put a bash script in /usr/lib/sendmail instead that should just read from standard input and send an email with the right headers:

#!/bin/bash

HEADERS="To: <myemail>
From: Cron <myotheremail>
Subject: Vixie-cron snooper ($@)

"

INPUT=$( cat /dev/stdin )

echo -e "$HEADERS""Stdin:\n$INPUT\n" | msmtp <myemail>
echo "$HEADERS""Stdin:\n$INPUT\n" > /tmp/vixielog

However, this doesn't have the desired effect. I just receive an almost empty email and /tmp/vixielog contains the same:

To: <myemail>
From: Cron <myotheremail>
Subject: Vixie-cron snooper (-i -FCronDaemon -oem <myemail>)

Stdin:

The emails come at the right time, so I know the cron job is being run properly, but I am not getting the output. How could I adjust this approach to get the output of the command in the email?

share|improve this question
1  
Does this help serverfault.com/questions/121121/… ? –  Iain Oct 16 '12 at 10:11
    
Thanks but that isn't what I'm looking for. I can send emails by wrapping each cron entry separately, but that is too cumbersome. I'm looking for a more elegant solution. –  robin Oct 16 '12 at 12:14

2 Answers 2

up vote 0 down vote accepted

Eventually I came to the following solution. Rather than using mstmp-mta, I wrote my own simple bash script that acts as my MTA. Placed in /usr/sbin/sendmail, it replaces the From header and sends the email on.

#!/bin/bash

sed -e "s/From: root (Cron Daemon)/From: WHATEVER YOU LIKE/" | msmtp $BASH_ARGV

Hopefully this helps anybody else who wants a lightweight solution to the problem.

share|improve this answer

I've made little wrapper for system mails, it parses /etc/aliases and send mail in correct format (through /usr/sbin/sendmail) :

#!/bin/bash
stdin=$(cat)

mail=$(echo "$stdin" | sed "s/From: root (Cron Daemon)/From: ${4} ${4}@domain.com/g")

if [ -f /etc/aliases ]; then
        if [ "grep ${1} /etc/aliases" != "" ]; then
                exec < /etc/aliases
                while read line
                do
                        if [[ $line =~ ^${1}:\ (.*)$ ]] ; then
                        addr=${BASH_REMATCH[1]}
                        else
                                if [[ $line =~ ^default:\ (.*)$ ]] ; then
                                        addr=${BASH_REMATCH[1]}
                                fi  
                        fi  
                done
                echo $addr
        fi 
else 
addr="default@domain.com"
fi

mail=$(echo "$mail" | sed "s/To: ${4}/To: ${addr} /g")

echo -e "$mail" | msmtp --auto-from=on --read-envelope-from --maildomain=domain.com -t 

Just change default mail and mail domain

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.