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

<p>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).</p>

<p>[EDIT]</p>

<pre>
$ mail -s Testing chris@example.org                                                                  
Cc: 
From: foo@bar.org

Testing
.
</pre>

<p>yields</p>

<pre>
Subject: Testing
To: &lt;chris@example.org&gt;
X-Mailer: mail (GNU Mailutils 1.1)
Message-Id: &lt;E1KdTJj-00025z-RK@localhost&gt;
From: &lt;chris@localhost&gt;
Date: Wed, 10 Sep 2008 13:17:23 -0400

From: foo@bar.org

Testing
</pre>

<p>The "From: foo@bar.org" line is part of the message body, not part of the header.</p>

[{'answer_id': 54752, 'author': 'Thomas Kammeyer', 'author_id': 4410, 'author_profile': 'https://Stackoverflow.com/users/4410', 'pm_score': 2, 'selected': False, 'text': '<p>Here are some options:</p>\n\n<ul>\n<li><p>If you have privelige enough, configure sendmail to do rewrites with the generics table</p></li>\n<li><p>Write the entire header yourself (or mail it to yourself, save the entire message with all headers, and re-edit, and send it with rmail from the command line</p></li>\n<li><p>Send directly with sendmail, use the "-f" command line flag and don\'t include your "From:" line in your message</p></li>\n</ul>\n\n<p>These aren\'t all exactly the same, but I\'ll leave it to you look into it further.</p>\n\n<p>On my portable, I have sendmail authenticating as a client to an outgoing mail server and I use generics to make returning mail come to another account.  It works like a charm.\nI aggregate incoming mail with fetchmail.</p>\n'}, {'answer_id': 380573, 'author': 'cms', 'author_id': 28532, 'author_profile': 'https://Stackoverflow.com/users/28532', 'pm_score': 8, 'selected': True, 'text': "<p>In my version of mail ( Debian linux 4.0 ) the following options work for controlling the source / reply addresses</p>\n\n<ul>\n<li>the <strong>-a</strong> switch, for additional headers to apply, supplying a From: header on the command line that will be appended to the outgoing mail header</li>\n<li>the <strong>$REPLYTO</strong> environment variable specifies a Reply-To: header</li>\n</ul>\n\n<p>so the following sequence</p>\n\n<pre><code>export REPLYTO=cms-replies@example.com\nmail -aFrom:cms-sends@example.com -s 'Testing'\n</code></pre>\n\n<p>The result, in my mail clients, is a mail from cms-sends@example.com, which any replies to will default to cms-replies@example.com</p>\n\n<p><em>NB:</em> Mac OS users: you don't have -a , but you do have <strong>$REPLYTO</strong></p>\n\n<p><em>NB(2):</em> CentOS users, many commenters have added that you need to use <code>-r</code> not <code>-a</code></p>\n\n<p><em>NB(3):</em> This answer is at least ten years old(1), please bear that in mind when you're coming in from Google. </p>\n"}, {'answer_id': 1353352, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>this worked for me </p>\n\n<pre><code>echo "hi root"|mail -rsawrub@testingdomain.org -s\'testinggg\' root\n</code></pre>\n'}, {'answer_id': 2059055, 'author': 'Beau', 'author_id': 60371, 'author_profile': 'https://Stackoverflow.com/users/60371', 'pm_score': 5, 'selected': False, 'text': '<p>On Centos 5.3 I\'m able to do:</p>\n\n<pre><code>mail -s "Subject" user@address.com -- -f from@address.com &lt; body\n</code></pre>\n\n<p>The double dash stops mail from parsing the -f argument and passes it along to sendmail itself.</p>\n'}, {'answer_id': 4519341, 'author': 'ubuntu-fanboy', 'author_id': 552429, 'author_profile': 'https://Stackoverflow.com/users/552429', 'pm_score': 5, 'selected': False, 'text': '<p>GNU mailutils\'s \'mail\' command doesn\'t let you do this (easily at least). But If you install \'heirloom-mailx\', its mail command (mailx) has the \'-r\' option to override the default \'$USER@$HOSTNAME\' from field.</p>\n\n<pre><code>echo "Hello there" | mail -s "testing" -r sender@company.com recipient@company.com\n</code></pre>\n\n<p>Works for \'mailx\' but not \'mail\'.</p>\n\n<pre>\n$ ls -l /usr/bin/mail\nlrwxrwxrwx 1 root root 22 2010-12-23 08:33 /usr/bin/mail -> /etc/alternatives/mail\n$ ls -l /etc/alternatives/mail\nlrwxrwxrwx 1 root root 23 2010-12-23 08:33 /etc/alternatives/mail -> /usr/bin/heirloom-mailx\n</pre>\n'}, {'answer_id': 6118894, 'author': 'gbla', 'author_id': 768791, 'author_profile': 'https://Stackoverflow.com/users/768791', 'pm_score': 2, 'selected': False, 'text': '<p>Thanks BEAU</p>\n\n<pre><code>mail -s "Subject" user@address.com -- -f from@address.com\n</code></pre>\n\n<p>I just found this and it works for me. The man pages for mail 8.1 on CentOS 5 doesn\'t mention this. For <code>-f</code> option, the man page says:</p>\n\n<blockquote>\n  <p>-f     Read messages from the file named by the file operand instead of the system mailbox.  (See  also  folder.)  If  no  file operand is specified, read messages from mbox instead of the system mailbox.</p>\n</blockquote>\n\n<p>So anyway this is great to find, thanks.</p>\n'}, {'answer_id': 6124477, 'author': 'Ryan Barong', 'author_id': 769550, 'author_profile': 'https://Stackoverflow.com/users/769550', 'pm_score': 2, 'selected': False, 'text': "<p>I don't know if it's the same with other OS, but in OpenBSD, the mail command has this syntax:</p>\n\n<pre><code>mail to-addr ... -sendmail-options ...\n</code></pre>\n\n<p>sendmail has -f option where you indicate the email address for the FROM: field. The following command works for me.</p>\n\n<pre><code>mail recepient@example.com -f from@example.com\n</code></pre>\n"}, {'answer_id': 8087751, 'author': 'papsy', 'author_id': 1040755, 'author_profile': 'https://Stackoverflow.com/users/1040755', 'pm_score': 0, 'selected': False, 'text': "<p>On CentOS 5.5, the easiest way I've found to set the default from domain is to modify the hosts file. If your hosts file contains your WAN/public IP address, simply modify the first hostname listed for it. For example, your hosts file may look like:</p>\n\n<blockquote>\n  <p>... <br>\n  11.22.33.44 localhost default-domain <strong>whatever-else.com</strong><br >\n  ... <br></p>\n</blockquote>\n\n<p>To make it send from whatever-else.com, simply modify it so that whatever-else.com is listed first, for example:</p>\n\n<blockquote>\n  <p>... <br>\n  11.22.33.44 <strong>whatever-else.com</strong> localhost default-domain <br>\n  ... <br></p>\n</blockquote>\n\n<p>I can't speak for any other distro (or even version of CentOS) but in my particular case, the above works perfectly. </p>\n"}, {'answer_id': 8483239, 'author': 'artickl', 'author_id': 1094841, 'author_profile': 'https://Stackoverflow.com/users/1094841', 'pm_score': 3, 'selected': False, 'text': '<p>Plus it\'s good to use <code>-F option</code> to specify Name of sender.</p>\n\n<p>Something like this:</p>\n\n<pre><code>mail -s "$SUBJECT" $MAILTO -- -F $MAILFROM -f ${MAILFROM}@somedomain.com\n</code></pre>\n\n<p>Or just look at available options:\n<a href="http://www.courier-mta.org/sendmail.html" rel="nofollow noreferrer">http://www.courier-mta.org/sendmail.html</a></p>\n'}, {'answer_id': 11656356, 'author': 'MoSs', 'author_id': 1552560, 'author_profile': 'https://Stackoverflow.com/users/1552560', 'pm_score': 4, 'selected': False, 'text': '<pre><code>mail -s "$(echo -e "This is the subject\\nFrom: Paula &lt;johny@paula.com&gt;\\n\nReply-to: 1232564@yourserver.com\\nContent-Type: text/html\\n")" \nmilas.josh@gmail.com &lt; htmlFileMessage.txt\n</code></pre>\n\n<p>the above is my solution....any extra headers can be added just after the from and before the reply to...just make sure you know your headers syntax before adding them....this worked perfectly for me.</p>\n'}, {'answer_id': 19007441, 'author': 'Alcanzar', 'author_id': 2785358, 'author_profile': 'https://Stackoverflow.com/users/2785358', 'pm_score': 3, 'selected': False, 'text': '<p>It\'s also possible to set both the From name and from address using something like:</p>\n\n<pre><code> echo test | mail -s "test" example@example.com -- -F\'Some Name&lt;example2@example.com&gt;\' -t\n</code></pre>\n\n<p>For some reason passing <code>-F\'Some Name\'</code> and <code>-fexample2@example.com</code> doesn\'t work, but passing in the <code>-t</code> to sendmail works and is "easy".</p>\n'}, {'answer_id': 21856390, 'author': 'Céline Aussourd', 'author_id': 2339082, 'author_profile': 'https://Stackoverflow.com/users/2339082', 'pm_score': 2, 'selected': False, 'text': '<p>On CentOS this worked for me: </p>\n\n<pre><code>echo "email body" | mail -s "Subject here" -r from_email_address email_address_to\n</code></pre>\n'}, {'answer_id': 24032988, 'author': 'keypress', 'author_id': 1961303, 'author_profile': 'https://Stackoverflow.com/users/1961303', 'pm_score': -1, 'selected': False, 'text': "<p>The answers provided before didn't work for me on CentOS5. I installed mutt. It has a lot of options. With mutt you do this this way:</p>\n\n<pre><code>export EMAIL=myfrom@example.com\nexport REPLYTO=myreplyto@example.com\nmutt -s Testing chris@example.org\n</code></pre>\n"}, {'answer_id': 26114710, 'author': 'deepak.prathapani', 'author_id': 2165897, 'author_profile': 'https://Stackoverflow.com/users/2165897', 'pm_score': 2, 'selected': False, 'text': '<p>I derived this from all the above answers. Nothing worked for me when I tried each one of them. I did lot of trail and error by combining all the above answers and concluded on this. I am not sure if this works for you but it worked for me on Ununtu 12.04 and RHEL 5.4.</p>\n\n<pre><code>echo "This is the body of the mail" | mail -s \'This is the subject\' \'&lt;receiver-id1@email.com&gt;,&lt;receiver-id2@email.com&gt;\' -- -F \'&lt;SenderName&gt;\' -f \'&lt;from-id@email.com&gt;\'\n</code></pre>\n\n<p>One can send the mail to any number of people by adding any number of receiver id\'s and the mail is sent by <strong>SenderName</strong> from <strong>from-id@email.com</strong></p>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 28788959, 'author': 'jelloir', 'author_id': 837163, 'author_profile': 'https://Stackoverflow.com/users/837163', 'pm_score': 2, 'selected': False, 'text': '<p>On Debian 7 I was still unable to correctly set the sender address using answers from this question, (would always be the hostname of the server) but resolved it this way.</p>\n\n<p>Install <strong>heirloom-mailx</strong></p>\n\n<pre><code>apt-get install heirloom-mailx\n</code></pre>\n\n<p>ensure it\'s the default.</p>\n\n<pre><code>update-alternatives --config mailx\n</code></pre>\n\n<p>Compose a message.</p>\n\n<pre><code>mail -s "Testing from &amp; replyto" -r "sender &lt;sender@example.com&gt;" -S replyto="sender@example.com" recipient@example.net &lt; &lt;(echo "Test message")\n</code></pre>\n'}, {'answer_id': 36642906, 'author': 'Andrew Backeby', 'author_id': 6208343, 'author_profile': 'https://Stackoverflow.com/users/6208343', 'pm_score': 1, 'selected': False, 'text': '<p><code>echo "body" | mail -S from=address@foo.com "Hello"</code></p>\n\n<p>-S lets you specify lots of string options, by far the easiest way to modify headers and such.</p>\n'}, {'answer_id': 38483319, 'author': 'J. Ceron', 'author_id': 1797498, 'author_profile': 'https://Stackoverflow.com/users/1797498', 'pm_score': 1, 'selected': False, 'text': '<p>echo "test" | mailx -r fake@example.com -s \'test\' email@example.com </p>\n\n<p>It works in OpenBSD.</p>\n'}, {'answer_id': 44888449, 'author': 'Stephane', 'author_id': 958373, 'author_profile': 'https://Stackoverflow.com/users/958373', 'pm_score': 0, 'selected': False, 'text': '<p>What allowed me to have a custom reply-to address on an <code>Ubuntu 16.04</code> with <code>UTF-8</code> encoding and a file attachment:</p>\n\n<p>Install the mail client:</p>\n\n<pre><code>sudo apt-get install heirloom-mailx\n</code></pre>\n\n<p>Edit the SMTP configuration:</p>\n\n<pre><code>sudo vim /etc/ssmtp/ssmtp.conf\nmailhub=smtp.gmail.com:587\nFromLineOverride=YES\nAuthUser=???@gmail.com\nAuthPass=???\nUseSTARTTLS=YES\n</code></pre>\n\n<p>Send the mail:</p>\n\n<pre><code>sender=\'send@domain.com\'\nrecipient=\'recipient@domain.com\'\nzipfile="results/file.zip"\ntoday=`date +\\%d-\\%m-\\%Y`\nmailSubject=\'My subject on the \'$today\nread -r -d \'\' mailBody &lt;&lt; EOM\nFind attached the zip file.\n\nRegards,\nEOM\nmail -s "$mailSubject" -r "Name &lt;$sender&gt;" -S replyto="$sender" -a $zipfile $recipient &lt; &lt;(echo $mailBody)\n</code></pre>\n'}, {'answer_id': 54215803, 'author': 'JazzCat', 'author_id': 972966, 'author_profile': 'https://Stackoverflow.com/users/972966', 'pm_score': 0, 'selected': False, 'text': '<p>None of the above solutions are working for me...</p>\n\n<pre><code>#!/bin/bash\n\n# Message\necho "My message" &gt; message.txt\n\n# Mail\nsubject="Test"\nmail_header="From: John Smith &lt;john.smith@example.com&gt;"\nrecipients="recipient@example.com"\n\n#######################################################################\ncat message.txt | mail -s "$subject" -a "$mail_header" -t "$recipients"\n</code></pre>\n'}, {'answer_id': 67505477, 'author': 'bluenote10', 'author_id': 1804173, 'author_profile': 'https://Stackoverflow.com/users/1804173', 'pm_score': 0, 'selected': False, 'text': '<p>I recent versions of <a href="https://mailutils.org/manual/html_section/mail.html" rel="nofollow noreferrer">GNU mailutils <code>mail</code></a> it is simply <code>mail -r foo@bar.com</code>.</p>\n<p>Looking at the raw sent mail, it seems to set both <code>Return-Path: &lt;foo@bar.com&gt;</code> and <code>From: foo@bar.com</code>.</p>\n'}]
['https://Stackoverflow.com/questions/54725', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1412/']