Chris Conway
Chris Conway

Reputation: 55989

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.

Upvotes: 107

Views: 297739

Answers (22)

SomeoneElse
SomeoneElse

Reputation: 479

You can configure mailutils like this:

Create a file /etc/mailutils.conf with the following contents:

address {
  email-domain somedomain.com;
};

Upvotes: 0

madyogi
madyogi

Reputation: 1

Quite an old one, but from internal mail CLI, typing help lists one of commands:

sen[dheader] [[header][: value]]

So:

? sendheader "From: My Name <[email protected]>"

And later typing r msgId to replay on it should bring the editor window and let one compose the mail. Remaining headers can be set from edit mode with '~' (tilde) key.

Ctrl+D terminates the input and makes mail to send the message.

Upvotes: 0

bluenote10
bluenote10

Reputation: 26560

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

Upvotes: 1

cms
cms

Reputation: 5982

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.

Upvotes: 122

JazzCat
JazzCat

Reputation: 4573

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"

Upvotes: 0

Stephane
Stephane

Reputation: 12750

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)

Upvotes: 0

J. Ceron
J. Ceron

Reputation: 1345

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

It works in OpenBSD.

Upvotes: 1

Andrew Backeby
Andrew Backeby

Reputation: 11

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.

Upvotes: 1

jalchy888
jalchy888

Reputation: 155

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")

Upvotes: 3

deepak.prathapani
deepak.prathapani

Reputation: 373

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.

Upvotes: 5

ubuntu-fanboy
ubuntu-fanboy

Reputation: 521

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

Upvotes: 29

artickl
artickl

Reputation: 81

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

Upvotes: 8

gbla
gbla

Reputation: 31

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.

Upvotes: 3

Alcanzar
Alcanzar

Reputation: 17155

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

Upvotes: 5

keypress
keypress

Reputation: 799

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]

Upvotes: -1

C&#233;line Aussourd
C&#233;line Aussourd

Reputation: 10664

On CentOS this worked for me:

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

Upvotes: 3

MoSs
MoSs

Reputation: 261

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.

Upvotes: 16

papsy
papsy

Reputation: 443

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.

Upvotes: 0

Ryan Barong
Ryan Barong

Reputation: 41

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]

Upvotes: 4

sawrub
sawrub

Reputation:

this worked for me

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

Upvotes: 0

Beau
Beau

Reputation: 11358

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.

Upvotes: 42

Thomas Kammeyer
Thomas Kammeyer

Reputation: 4507

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.

Upvotes: 4

Related Questions