Change the "From:" address in Unix "mail"
UnixEmailUnix Problem Overview
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]yields
Cc: From: [email protected]Testing .
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 -0400The "From: [email protected]" line is part of the message body, not part of the header.From: [email protected]
Testing
Unix Solutions
Solution 1 - Unix
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 REPLYTO=cms-replies@example.com
mail -aFrom:cms-sends@example.com -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.
Solution 2 - Unix
On Centos 5.3 I'm able to do:
mail -s "Subject" user@address.com -- -f [email protected] < body
The double dash stops mail from parsing the -f argument and passes it along to sendmail itself.
Solution 3 - Unix
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 sender@company.com recipient@company.com
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
Solution 4 - Unix
mail -s "$(echo -e "This is the subject\nFrom: Paula <johny@paula.com>\n
Reply-to: 1232564@yourserver.com\nContent-Type: text/html\n")"
milas.josh@gmail.com < 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.
Solution 5 - Unix
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
Solution 6 - Unix
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".
Solution 7 - Unix
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.
Solution 8 - Unix
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.
Solution 9 - Unix
Thanks BEAU
mail -s "Subject" user@address.com -- -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.
Solution 10 - Unix
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 recepient@example.com -f from@example.com
Solution 11 - Unix
On CentOS this worked for me:
echo "email body" | mail -s "Subject here" -r from_email_address email_address_to
Solution 12 - Unix
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")
Solution 13 - Unix
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.
Solution 14 - Unix
echo "test" | mailx -r [email protected] -s 'test' [email protected]
It works in OpenBSD.
Solution 15 - Unix
this worked for me
echo "hi root"|mail [email protected] -s'testinggg' root
Solution 16 - Unix
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.
Solution 17 - Unix
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
AuthUser[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)
Solution 18 - Unix
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"
Solution 19 - Unix
I recent versions of GNU mailutils mail
it is simply mail -r [email protected]
.
Looking at the raw sent mail, it seems to set both Return-Path: <[email protected]>
and From: [email protected]
.
Solution 20 - Unix
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=myfrom@example.com
export REPLYTO=myreplyto@example.com
mutt -s Testing chris@example.org