3

How to change the mail FROM address in Red Hat Linux 6? I send a mail by using this script:

TO_ADDRESS="myfriendaddress@gmail.com"
FROM_ADDRESS="myaddress@gmail.com"
SUBJECT="SERVER FAILURE"
BODY="SERVER IS FAILED NOW"

echo ${BODY}| mail -s ${SUBJECT} ${TO_ADDRESS} -- -r ${FROM_ADDRESS}

But the email address in the FROM field is always root@redhat.localdomain How to change the FROM address to something like myaddress@gmail.com

  • Try moving the -r ... to just after mail. Also, it depends on which mail client you are using. What does rpm -qf /usr/bin/mail say? – meuh May 26 '16 at 7:57
  • If you don't want mail to stop processing options, then don't put -- before -r. Also, you really should double-quote your variables, even if they are in curly-braces - curly braces are not a substitute for proper variable quoting, they are for disambiguating a var from surrounding text (e.g. ${foo}123 means the contents of $foo followed by 123 while $foo123 means the contents of variable $foo123). `"${SUBJECT}" and "${BODY}" especially need it as they contain unescaped space characters. but double-quote ALL of them, not just $SUBJECT and $BODY. – cas May 26 '16 at 9:22
4

You can specify any extra header you may need with -a

$echo "This is the main body of the mail" | mail -s "Some random subject" -a "From: some@domain.com" to@domain.com

Alternative Solution

In my version of mail, the following options work for controlling the source / reply addresses

  • the -a switch, for additional headers to apply, supplying a From: header on the command line that will be appended to the outgoing mail header
  • the $REPLYTO environment variable specifies a Reply-To: header

so the following sequence

export REPLYTO=to@example.com
mail -aFrom:from@example.com -s 'Testing'

The result, in my mail clients, is a mail from from@example.com, which any replies to will default to to@example.com

NOTE by @Rui F Ribeiro: only the root user is able to change the From field.

  • 2
    +1 for the post. I also suggest you adding to your answer that with this method, only the root user is able to change the From field. Mainly because developers often have a hard time understanding why with sendmail compatible APIs they are not able to change the From address, and while talking via a TCP based API/function, they can change it at will. – Rui F Ribeiro May 26 '16 at 8:48
  • @RuiFRibeiro Thanks for your suggestion. Updated the answer. You can update my answer for more details if you want. – Rahul May 26 '16 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.