How to change "From:" field for emails from Cron?

63,258

Solution 1

I don't think you can change the FROM address, (someone should add a MAILFROM option).

You can do something like this though to achieve a similar result:

* * * * * /path/to/script 2>&1 | mail -s "Output of /path/to/script" [email protected] -- -r "[email protected]" -F"Full Name of sender"

All output is piped to the mail command so the MAILTO variable isn't used at all.

The to address would need to be set but you may be able to use $MAILTO variable. The -- sets the rest of the options to be sendmail options so you can use the -r and and -F options.

-s is the subject

-r is the reply address

-F is the Full name of the sender (makes it look nice in email clients)

Solution 2

Modern versions of cron do accept "MAILFROM=..." in the crontab format. I suggest that you try "man 5 crontab". If it mentions MAILFROM, your version should support it. The phrase to look for is towards the end of the paragraph discussing MAILTO, and should be something like this:

If MAILFROM is defined (and non-empty), it will be used as the envelope sender address, otherwise, ''root'' will be used.

Solution 3

/etc/mailname contains the domain name part of the FROM address. If /etc/mailname contains 'somecompany.com' then cron running for root would have sender as [email protected]

Solution 4

For me, the easiest way to change the from address on a system, is to create a ~/.mailrc file with contents like this:

set name="My Full Name"
set from="[email protected]"

Any of the mail commands that run as my user, now use these settings.

Solution 5

You can set the nullmailer from address via environment variables or command line. The command line arguments are -f and -F for sender address and full name respectively.

Usually you can set environment variables in the crontab.

NULLMAILER_USER=webmaster
NULLMAILER_HOST=host.example.com
NULLMAILER_NAME="Mr Cron"

5 0 * * * /usr/local/bin/daily.sh
Share:
63,258

Related videos on Youtube

dsilva.vinicius
Author by

dsilva.vinicius

I like python, django and linux. Also, I'm a big fan of TDD.

Updated on September 17, 2022

Comments

  • dsilva.vinicius
    dsilva.vinicius over 1 year

    I use remote SMTP via nullmailer and it requires set From field to the specific name, but cron set it as [email protected].

    How could I change it to something like [email protected]?

  • Kimvais
    Kimvais about 14 years
    He's asking about FROM: not TO:
  • dsilva.vinicius
    dsilva.vinicius about 14 years
    Hm, this does not work for unknown reason.
  • Flimzy
    Flimzy over 11 years
    Where "modern" means what? This is not supported by the most recent version of cron in Debian unstable (3.0pl1-124) from what I can see in the changelog or the documentation.
  • Flimzy
    Flimzy over 11 years
    This isn't actually true--at least not in many cases. On my system, for instance, it is only sent from 'user', not 'user@domain'. It is the responsibility of the MTA to add the local domain name. This is significant in some cases (such as mine) where my MTA (for reasons too complicated and boring to describe) is not adding the domain name.
  • RuiDC
    RuiDC over 10 years
    thx, this worked for me on Archlinux. There are many different flavours of cron. Debian's ISC is from 2004, so stretching the definition of "modern", but with no info on what system it's being used on - hard to provide that detail!
  • Arto Bendiken
    Arto Bendiken over 9 years
    Unfortunately, Ubuntu's cron (at least as of 14.04 LTS) has no mention of MAILFROM in man 5 crontab.
  • Zitrax
    Zitrax over 9 years
    Some info here: wiki.debian.org/EtcMailName - does not say if nullmailer use it. I use sendmail and it did not seem to work for it.
  • Andreas Klöckner
    Andreas Klöckner over 8 years
    You can install cronie to replace cron on Debian/Ubuntu. Ta-daa: MAILFROM exists:
  • M. Barabas
    M. Barabas about 8 years
    Ubuntu cron of 16.04 doesn't have MAILFROM option
  • M. Barabas
    M. Barabas about 8 years
    cronie doesn't exist on Ubuntu 16.04
  • lkraav
    lkraav over 5 years
    serverfault.com/a/437319/30697 should probably be the Accepted answer.
  • FGM
    FGM over 3 years
    MAILFROM is still missing in Ubuntu 20.04.
  • Houman
    Houman about 3 years
    Doesn't work on Debian Buster 10.9
  • piec
    piec over 2 years
    Also described here: serverfault.com/a/1078925/215766
  • Jota Be
    Jota Be about 2 years
    MAILFROM finally exists in Ubuntu 22.04. 10 years later.
  • Admin
    Admin almost 2 years
    We are exactly in the case of xkcd.com/979 What did you do exactly?