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:


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>)


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?

  • 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

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.


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.


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


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
                        if [[ $line =~ ^${1}:\ (.*)$ ]] ; then
                                if [[ $line =~ ^default:\ (.*)$ ]] ; then
                echo $addr

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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