Linux / UNIX: Change Crontab Email Settings ( MAILTO )

by Vivek Gite on September 20, 2008 · 9 comments

Q. I'd like to send email to user@example.com instead of default root user. How do I change email settings under crontab file?

A. A crontab file contains instructions to the cron daemon.

An active line in a crontab will be either an environment setting or a cron command. An environment setting is of the form:

 name = value

where the spaces around the equal-sign (=) are optional, and any subsequent non-leading spaces in value will be part of the value assigned to name. The value string may be placed in quotes (single or double, but matching) to preserve leading or trailing blanks. The value string is not parsed for environmental substitutions, thus lines like

PATH = $HOME/bin:$PATH

will not work as you might expect.

MAILTO Variable

In addition to LOGNAME, HOME, and SHELL, cron will look at MAILTO if it has any reason to send mail as a result of running commands in "this" crontab. If MAILTO is defined (and non-empty), mail is sent to the user so named. First open your crontab file:
# vi /etc/crontab
OR
$ crontab -e
To send email to vivek@nixcraft.in, enter:
MAILTO=vivek@nixcraft.in

If MAILTO is defined but empty (MAILTO=""), no mail will be sent.
MAILTO=""

Otherwise mail is sent to the owner of the crontab.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 9 comments… read them below or add one }

1 Brock Tice September 20, 2008

Thanks, never even thought it could be changed, I always used my .forward for this. Very handy.

Reply

2 Darren September 20, 2008

I too have always the “.forward” option. This is done by the follwing…..

create a new file called .forward
vi /root/.forward

Enter the email address on the first line in the file.
youremail@address.com

Save (w) the file and exit (q)
:wq

Now, anytime the ‘root’ user get an email, it will automatically be forwarded to the address you specified in the .forward file.

You can also place the .forward file in users home directories /home/user1/.forward

Thanks for the info up above though, I’ll have to check that out.

Reply

3 phanikumar November 26, 2008

Hi All,

Please help me out..

I configured cron job under FTPPKR user.i’ll get mails from FTPPKR@xyz.com whenever cron runs..But whenever i need the mails to be delivered from GTPPKR@xyz.com user.

Is it possibe? If yes, please help me out..

Reply

4 Andre July 4, 2009

Yes, that’s entirely possible. Just figure out if the emails are being sent with sendmail or postfix, then adjust their settings to reflect that. You could deploy address rewrites to achieve this, for example.

Reply

5 maxo May 7, 2010

SOLARIS does not support the MAILTO feature. Instead one needs to append the crontab entry with | mailto user@domain
Ex:
00 12 * * * run_command.sh | mail user@domain

Reply

6 Dmitri Minaev September 1, 2010

Mail addressed to ‘root’ is often redirected to some other account using /etc/aliases. You might like to edit (or add) the line beginning with ‘root:’ to point to some other account.

Reply

7 alex July 3, 2011

But, How if I dont want to receive notification from crontab? How to set it ? Because I’m monitoring mail server, and notification from crontab unnecessary for me.

Reply

8 martijn July 14, 2011

@alex: add >/dev/null 2>&1 to the end of your line(s) in crontab. This will redirect all crontab output to /dev/null

Reply

9 mehturt July 26, 2011

What command is cron using to send email? I’m trying to get cron working with esmtp, but no luck so far.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 11 + 2 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: