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?
Best Answer
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.Hopefully this helps anybody else who wants a lightweight solution to the problem.