26

I'm running a script on my mybookworld(NAS) via crontab every day. And it would be nice if it could send me everyday an email report with the output of the script.

So I looked at MAILTO in crontabs, but it doesn't send me anything at all. The script ran correctly at the right time, but there was no email. This is my crontab:

~ # crontab -l
MAILTO=x.y@googlemail.com

0 0 * * * python /root/erepCrawler/src/main.py

I've written a "smaller" crontab for testing reasons:

MAILTO=x.y@googlemail.com
* * * * * echo "blaah"

This should send me every minute an email with "blaah" as the body. If I'm wrong please correct me.

Is there another package needed for sending mails with crontab? sendmail is installed in /opt/sbin/sendmail.

rgettman
  • 176,041
  • 30
  • 275
  • 357
Simon Lenz
  • 2,732
  • 5
  • 33
  • 39
  • 12
    I guess you didn't set up any mail transport agent appropriately, did you? Can you send mails from command line, e.g. `echo Test | mail -s Test x.y@googlemail.com` – bmk Mar 25 '11 at 21:38
  • 2
    i think you are right. There is not even installed something named mail. After a bit of googling i'm trying now to install postfix on the Nas. With this, i should work right? – Simon Lenz Mar 26 '11 at 03:13
  • If postfix is configured correctly (expecially relay server etc.) it should work. – bmk Mar 26 '11 at 17:04

3 Answers3

14

It should work in the cron with following modifications:

MAILTO="x.y@googlemail.com"
* * * * * echo blaah

works on my server, just tested.

Scherbius.com
  • 3,396
  • 4
  • 24
  • 44
  • Well, i think the " isn't the point (see bml's comment). But it is good to know, that the `echo "blaah"` works if the mail thing is installed correctly:) thanks for the help – Simon Lenz Mar 26 '11 at 03:15
  • 1
    are the quotes needed? I am trying to debug my problem. – crh225 Apr 01 '13 at 14:27
  • 2
    Quotes not needed, the following is perfectly valid MAILTO=email1@domain.com,email2@domain.com – zsoobhan Dec 16 '14 at 11:19
  • 5
    Quotes needed for no email! Took quite a while to figure out. – trss Jun 13 '17 at 08:01
  • I did tried without quotes and It works as all email users received the email. In my case, it wasn't working because I was emailing the output and trying to write it to a file at the same time which means the following code, it won't work. `MAILTO=me@example.com,coleague@example.com` `* * * * * echo 'blah' >> /tmp/logs/blah.log` – Sylvester Loreto Nov 09 '18 at 12:21
  • You need to have a working MTA (mail transport agent) installed and configured for the MAILTO to work properly – der_michael Feb 08 '22 at 21:30
2

after spending hours debugging why this wasn't working with nullmailer on my Debian box, I discovered I had a filter at gmail sending everything from cron to "All Mail" without going through my inbox... just something to check.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
1

I had to add this to /etc/hosts so that mail to anyone at my server's own FQDN would be resolved:

127.0.0.1 mydomain.com.

Notice the dot at the end. In my case, I'm sending email essentially to root@localhost with MAILTO=root which gets translated to root@mydomain.com.

Here's some more detail on this answer.

hamx0r
  • 4,081
  • 1
  • 33
  • 46