How to Fix MAILTO Not Working in CRON: Email Notification Issues in Linux


2 views

When setting up cron jobs, email notifications via MAILTO are crucial for monitoring job executions. The issue occurs when:

Feb 23 14:13:01 internal crond[6857]: (root) MAIL (mailed 79 bytes of output but got status 0x0001)

This indicates the cron daemon attempted to send mail but encountered errors despite having mailx and sendmail installed.

  • Works when MAILTO is set in user crontab (crontab -e)
  • Fails when set in /etc/crontab
  • Command line email sending functions properly

The behavior difference stems from how cron handles environment variables:

# System-wide crontab (/etc/crontab) requires user specification:
MAILTO=admin@example.com
* * * * * root /path/to/script.sh

# User crontabs apply MAILTO globally for all jobs

Option 1: System-wide Configuration

For /etc/crontab to work with MAILTO:

# Install required packages
sudo yum install postfix mailx

# Configure mail transport
sudo alternatives --set mta /usr/sbin/sendmail.postfix
sudo systemctl restart postfix

Option 2: User-specific Approach

For individual user crontabs:

crontab -e
# First line sets recipient
MAILTO=devops@example.com
# Subsequent lines define jobs
*/5 * * * * /home/user/backup.sh

Check these critical components:

# Verify mail queue
mailq

# Test email functionality
echo "Test body" | mail -s "Test Subject" admin@example.com

# Examine mail logs
tail -f /var/log/maillog

For complex setups requiring custom mail handling:

# /etc/crontab example with full path to mail binary
MAILTO="admin@example.com"
MAILFROM="cron@server.com"
* * * * * root /usr/bin/mail -s "Cron Alert" $MAILTO < /var/log/cron.log
  • Missing package dependencies (mailutils, postfix/sendmail)
  • Incorrect file permissions on /usr/bin/mail
  • Firewall blocking SMTP traffic (port 25)
  • Improper FQDN configuration in /etc/hosts

For production systems, consider redirecting cron mail to external services:

# Using ssmtp with Gmail
MAILTO="team-alerts@company.com"
*/30 * * * * root /usr/bin/curl -X POST -d "$(cat /var/log/cronjob.log)" https://hooks.slack.com/services/...

When setting up cron jobs, email notifications are crucial for monitoring task execution. Many administrators encounter situations where the MAILTO directive doesn't work as expected, particularly when configured in /etc/crontab versus user-specific crontabs.

There are several ways to configure cron jobs, each with different behaviors regarding email notifications:

1. System-wide crontab (/etc/crontab)
2. User-specific crontabs (crontab -e)
3. Files in /etc/cron.d/ directory

The fundamental issue lies in how different crontab files process environment variables:

# System crontab (/etc/crontab) requires user specification
MAILTO=admin@example.com
* * * * * root /path/to/script.sh

# User crontab processes MAILTO differently
MAILTO=admin@example.com
* * * * * /path/to/script.sh

For reliable email delivery from cron jobs, ensure these components are properly configured:

# Install required packages (CentOS/RHEL)
yum install -y mailx postfix

# Verify sendmail compatibility
alternatives --config mta

# Test email functionality
echo "Test message" | mail -s "Test Subject" admin@example.com

Check these log files when troubleshooting email delivery issues:

/var/log/cron - for cron-specific errors
/var/log/maillog - for mail delivery attempts
/var/spool/mail/root - system mail spool

For more control over email delivery, consider these approaches:

# Custom mail command in crontab
MAILTO=""
* * * * * /path/to/script.sh 2>&1 | mail -s "Cron Output" admin@example.com

# Using external mail services
MAILTO="admin@example.com"
MAILFROM="cron@yourdomain.com"

When configuring CRON email notifications:

  • Use dedicated email accounts for monitoring
  • Implement proper SPF/DKIM records
  • Consider encrypted email delivery for sensitive output
  • Set appropriate mail storage quotas