Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the "From:" address in Unix "mail"

Tags:

unix

email

Sending a message from the Unix command line using mail TO_ADDR results in an email from $USER@$HOSTNAME. Is there a way to change the "From:" address inserted by mail?

For the record, I'm using GNU Mailutils 1.1/1.2 on Ubuntu (but I've seen the same behavior with Fedora and RHEL).

[EDIT]

 $ mail -s Testing [email protected]                                                                   Cc:  From: [email protected]  Testing . 

yields

 Subject: Testing To: <[email protected]> X-Mailer: mail (GNU Mailutils 1.1) Message-Id: <E1KdTJj-00025z-RK@localhost> From: <chris@localhost> Date: Wed, 10 Sep 2008 13:17:23 -0400  From: [email protected]  Testing 

The "From: [email protected]" line is part of the message body, not part of the header.

like image 227
Chris Conway Avatar asked Sep 10 '08 17:09

Chris Conway


People also ask

How do I change the sender name in mailx?

The Usage info shows "[-- sendmail-options ...]" and since "-r" is a sendmail option, you need to use the double dashes first. Show activity on this post. The double-dash before "-f" makes the mailx not to parse the -f, but only pass it to sendmail/postfix, which will then send with the "from" address mentioned.

How do I add sender address in mailx?

You can use the "-r" option to set the sender address: mailx -r [email protected] -s ... This is troubling for me because the man page of mailx says: "-r address Sets the From address. Overrides any from variable specified in environment or startup files.


2 Answers

In my version of mail ( Debian linux 4.0 ) 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 [email protected] mail -aFrom:[email protected] -s 'Testing' 

The result, in my mail clients, is a mail from [email protected], which any replies to will default to [email protected]

NB: Mac OS users: you don't have -a , but you do have $REPLYTO

NB(2): CentOS users, many commenters have added that you need to use -r not -a

NB(3): This answer is at least ten years old(1), please bear that in mind when you're coming in from Google.

like image 161
cms Avatar answered Oct 23 '22 01:10

cms


On Centos 5.3 I'm able to do:

mail -s "Subject" [email protected] -- -f [email protected] < body 

The double dash stops mail from parsing the -f argument and passes it along to sendmail itself.

like image 22
Beau Avatar answered Oct 23 '22 01:10

Beau