How to Override Cron Job Email’s “From” Field When Using Nullmailer with Remote SMTP


2 views

When cron jobs send email notifications through nullmailer configured with remote SMTP, the default "From" address often shows as root@hostname, which can cause delivery issues if your SMTP provider requires specific sender authorization.

Most SMTP services (like Gmail, SendGrid, or AWS SES) enforce strict sender verification. If your cron's From header doesn't match an authorized address, emails either get rejected or marked as spam.

Nullmailer respects the MAILFROM environment variable. Add this to your crontab before commands:

MAILFROM="yourname@domain.com"
0 * * * * /path/to/your/script.sh

Edit /etc/nullmailer/remotes and append the envelope sender after your SMTP settings:

smtp.yourprovider.com smtp --port=587 --user=login --pass=password --ssl
  --from="Your Name <yourname@domain.com>"

Create /etc/mailname containing just your domain:

yourdomain.com

Verify with this cron job (temporarily):

* * * * * echo "Test email" | mail -s "Cron From Test" recipient@example.com
  • Check /var/log/mail.log for delivery errors
  • Use nullmailer-inject --verbose recipient@example.com for testing
  • Ensure your SMTP provider allows the From address you're trying to use

When cron jobs send email notifications through nullmailer with remote SMTP, many systems default to using root@hostname as the sender address. This becomes problematic when:

  • Your SMTP provider requires specific authorized sender addresses
  • Corporate email filters block "root@" addresses
  • You want recipient filtering based on sender domain

The key configuration file is /etc/nullmailer/remotes which defines SMTP server details. But for From address control, we need to modify additional files:

# Example remotes file content
smtp.yourprovider.com smtp --port=587 --starttls --user=me@ya.ru --pass=mypassword

Create or edit /etc/nullmailer/defaultfrom:

me@ya.ru

For per-job control, set the MAILFROM environment variable in your cron job:

0 * * * * MAILFROM="service-alerts@ya.ru" /path/to/script.sh

For complete control, use formail in your cron command:

0 * * * * /path/to/script.sh | formail -a "From: My Service <me@ya.ru>" | sendmail -t

This approach gives you:

  • Custom display names
  • Different addresses per job
  • Header consistency with your domain

If changes don't take effect:

  1. Restart nullmailer: sudo systemctl restart nullmailer
  2. Check mail logs: journalctl -u nullmailer
  3. Verify file permissions (should be root:mail 640)