[unix] Change the "From:" address in Unix "mail"

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.

This question is related to unix email

The answer is


On CentOS this worked for me:

echo "email body" | mail -s "Subject here" -r from_email_address email_address_to

I don't know if it's the same with other OS, but in OpenBSD, the mail command has this syntax:

mail to-addr ... -sendmail-options ...

sendmail has -f option where you indicate the email address for the FROM: field. The following command works for me.

mail [email protected] -f [email protected]

mail -s "$(echo -e "This is the subject\nFrom: Paula <[email protected]>\n
Reply-to: [email protected]\nContent-Type: text/html\n")" 
[email protected] < htmlFileMessage.txt

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.


Here are some options:

  • If you have privelige enough, configure sendmail to do rewrites with the generics table

  • 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

  • Send directly with sendmail, use the "-f" command line flag and don't include your "From:" line in your message

These aren't all exactly the same, but I'll leave it to you look into it further.

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. I aggregate incoming mail with fetchmail.


this worked for me

echo "hi root"|mail [email protected] -s'testinggg' root

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:

...
11.22.33.44 localhost default-domain whatever-else.com
...

To make it send from whatever-else.com, simply modify it so that whatever-else.com is listed first, for example:

...
11.22.33.44 whatever-else.com localhost default-domain
...

I can't speak for any other distro (or even version of CentOS) but in my particular case, the above works perfectly.


echo "body" | mail -S [email protected] "Hello"

-S lets you specify lots of string options, by far the easiest way to modify headers and such.


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:

export [email protected]
export [email protected]
mutt -s Testing [email protected]

None of the above solutions are working for me...

#!/bin/bash

# Message
echo "My message" > message.txt

# Mail
subject="Test"
mail_header="From: John Smith <[email protected]>"
recipients="[email protected]"

#######################################################################
cat message.txt | mail -s "$subject" -a "$mail_header" -t "$recipients"

What allowed me to have a custom reply-to address on an Ubuntu 16.04 with UTF-8 encoding and a file attachment:

Install the mail client:

sudo apt-get install heirloom-mailx

Edit the SMTP configuration:

sudo vim /etc/ssmtp/ssmtp.conf
mailhub=smtp.gmail.com:587
FromLineOverride=YES
[email protected]
AuthPass=???
UseSTARTTLS=YES

Send the mail:

sender='[email protected]'
recipient='[email protected]'
zipfile="results/file.zip"
today=`date +\%d-\%m-\%Y`
mailSubject='My subject on the '$today
read -r -d '' mailBody << EOM
Find attached the zip file.

Regards,
EOM
mail -s "$mailSubject" -r "Name <$sender>" -S replyto="$sender" -a $zipfile $recipient < <(echo $mailBody)

Thanks BEAU

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

I just found this and it works for me. The man pages for mail 8.1 on CentOS 5 doesn't mention this. For -f option, the man page says:

-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.

So anyway this is great to find, thanks.


It's also possible to set both the From name and from address using something like:

 echo test | mail -s "test" [email protected] -- -F'Some Name<[email protected]>' -t

For some reason passing -F'Some Name' and [email protected] doesn't work, but passing in the -t to sendmail works and is "easy".


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.


Plus it's good to use -F option to specify Name of sender.

Something like this:

mail -s "$SUBJECT" $MAILTO -- -F $MAILFROM -f ${MAILFROM}@somedomain.com

Or just look at available options: http://www.courier-mta.org/sendmail.html


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.

echo "Hello there" | mail -s "testing" -r [email protected] [email protected]

Works for 'mailx' but not 'mail'.

$ ls -l /usr/bin/mail
lrwxrwxrwx 1 root root 22 2010-12-23 08:33 /usr/bin/mail -> /etc/alternatives/mail
$ ls -l /etc/alternatives/mail
lrwxrwxrwx 1 root root 23 2010-12-23 08:33 /etc/alternatives/mail -> /usr/bin/heirloom-mailx

echo "test" | mailx -r [email protected] -s 'test' [email protected]

It works in OpenBSD.


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.

echo "This is the body of the mail" | mail -s 'This is the subject' '<[email protected]>,<[email protected]>' -- -F '<SenderName>' -f '<[email protected]>'

One can send the mail to any number of people by adding any number of receiver id's and the mail is sent by SenderName from [email protected]

Hope this helps.


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.

Install heirloom-mailx

apt-get install heirloom-mailx

ensure it's the default.

update-alternatives --config mailx

Compose a message.

mail -s "Testing from & replyto" -r "sender <[email protected]>" -S replyto="[email protected]" [email protected] < <(echo "Test message")