mail Linux command include username along with from address [duplicate]

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.