morleyc
morleyc

Reputation: 2431

Set from address text in bash mail

I am able to send mail from bash, but cannot change the from text, it currently comes up as 'root'.

I would like email clients to show the following text in the from field script on myserver.com ([email protected]).

How can I do this please?

emailsubject="$scriptname ($scriptver) log at $startdate"

if [ $errorcount > 0 ]; then
    emailsubject="ERRORS($errorcount) - ${emailsubject}"
fi

mail -s "$emailsubject" [email protected] < $logfile

Upvotes: 5

Views: 20827

Answers (2)

Roman Hocke
Roman Hocke

Reputation: 4239

If Your "mail" supports it, You may add custom headers using the -a switch like this:

mail -s "subject" -a "From: [email protected]" ...

Upvotes: 14

David W.
David W.

Reputation: 107040

This isn't a BASH issue. This is an issue with the mail program itself.

Run the command man mail and see the options for the command. It varies from system to system. Check for a parameter that allows you to set who the mail is from or the sender. If mail doesn't have such a parameter, try mailx which has more options. (It looks like your system has mailx called mail.

On my system, I use mailx instead of mail and I can use the -r parameter to set a sender address:

mailx -r [email protected] -s "Secret Message. For your eyes only" < $file

Upvotes: 6

Related Questions