更改 Unix“ mail”中的“ From:”地址

使用 mail TO_ADDR从 Unix 命令行发送消息将导致来自 $USER@$HOSTNAME的电子邮件。有没有办法更改由 mail插入的“ From:”地址?

郑重声明,我在 Ubuntu 上使用了 GNU Mailutils 1.1/1.2(但我在 Fedora 和 RHEL 上看到了同样的行为)。

[编辑]

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


Testing
.

产量

Subject: Testing
To: <chris@example.org>
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: foo@bar.org


Testing

“ From: foo@bar.org”行是消息正文的一部分,而不是标题的一部分。

275974 次浏览

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.

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 cms-sends@example.com, which any replies to will default to cms-replies@example.com

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.

this worked for me

echo "hi root"|mail -rsawrub@testingdomain.org -s'testinggg' root

On Centos 5.3 I'm able to do:

mail -s "Subject" user@address.com -- -f from@address.com < body

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

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

Thanks BEAU

mail -s "Subject" user@address.com -- -f from@address.com

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.

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

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.

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

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.

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

 echo test | mail -s "test" example@example.com -- -F'Some Name<example2@example.com>' -t

For some reason passing -F'Some Name' and -fexample2@example.com doesn't work, but passing in the -t to sendmail works and is "easy".

On CentOS this worked for me:

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

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

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' '<receiver-id1@email.com>,<receiver-id2@email.com>' -- -F '<SenderName>' -f '<from-id@email.com>'

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 from-id@email.com

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 <sender@example.com>" -S replyto="sender@example.com" recipient@example.net < <(echo "Test message")

echo "body" | mail -S from=address@foo.com "Hello"

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

echo "test" | mailx -r fake@example.com -s 'test' email@example.com

It works in OpenBSD.

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=???@gmail.com
AuthPass=???
UseSTARTTLS=YES

Send the mail:

sender='send@domain.com'
recipient='recipient@domain.com'
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)

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 <john.smith@example.com>"
recipients="recipient@example.com"


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

I recent versions of GNU mailutils mail it is simply mail -r foo@bar.com.

Looking at the raw sent mail, it seems to set both Return-Path: <foo@bar.com> and From: foo@bar.com.