使用mail命令发送电子邮件时指定发件人

有谁知道如何更改从用户发送电子邮件时使用邮件命令? 我已经通过手册页,并不能看到如何做到这一点。

我们正在运行Redhat Linux 5。

http://www.mindspill.org/962似乎有一个解决scheme。;

主要有:

echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -- -f from_user@example.com

您可以使用-a指定任何额外的标头

 $mail -s "Some random subject" -a "From: some@mail.tld" to@mail.tld 

mail -r from@from.from -R from@from.com

-r = from-addr -R =回复地址

作者表示他的邮件版本不支持这个标志。 但是,如果你有一个这样的工作正常的版本。

当通过SMTP发送时, mail手册页build议设置fromvariables,这样:

 mail -s Subject -S from=sender@example.com recipient@example.com 

否则,使用-r选项。

在CentOS 6上testing

您也可以使用以下语法附加一个文件:
mail -s Subject -S from=sender@example.com recipient@example.com <absolute_path_of_file_to_be_attached>

这些都没有为我工作(Ubuntu 12.04),但最后与试验和错误我得到了:

 echo 'my message blabla\nSecond line (optional of course)' | mail -s "Your message title" -r 'Your full name<yourSenderAdress@yourDomain.abc>' -Sreplyto="yourReplyAdressIfDifferent@domain.abc" destinatorEmail@destDomain.abc[,otherDestinator@otherDomain.abc] 

(全部在一行中,“-Sreplyto”中没有空格)

我从这里得到这个邮件命令:

 apt-get install mailutils 

您可以通过首先添加 – 将sendmail选项附加到邮件命令的末尾。 -f是sendmail上用于设置发件人地址的命令。 所以你可以这样做:

邮箱recipient@foo.com – -f sender@bar.com

这是一个解决scheme 。

第二个最简单的解决scheme之后-r(这是指定一个From:头,并通过一个换行像这样分隔它

  $mail -s "Subject" destination@example.com From: Joel <joel@example.com> Hi! . 

仅在less数邮件版本中工作,不知道redhat的版本是什么)。

PS:大多数版本的邮件吸!

以上都没有为我工作。 我花了很长时间才弄明白,希望这能帮助下一个人。

我正在使用Ubuntu 12.04 LTS和mailutils v2.1。

我在网上find了这个解决scheme,不知道在哪里,找不到了:

 -aFrom:Servername-Server@mydomain.com 

完整命令使用:

 cat /root/Reports/ServerName-Report-$DATE.txt | mail -s "Server-Name-Report-$DATE" myemailadress@mydomain.com -aFrom:Servername-Server@mydomain.com 

在尝试正确伪造电子邮件的发件人地址时,大多数人需要更改两个值。 首先是从地址,第二是原始地址。 网上提供的许多解决scheme只会改变其中的一个值。

如果是root用户,我尝试一个简单的邮件命令来发送自己的电子邮件,看起来像这样。 echo "test" | mail -s "a test" me@noone.com

和相关的日志: Feb 6 09:02:51 myserver postfix/qmgr[28875]: B10322269D: from=<root@myserver.com>, size=437, nrcpt=1 (queue active) Feb 6 09:02:52 myserver postfix/smtp[19848]: B10322269D: to=<me@noone.com>, relay=myMTA[xxxx]:25, delay=0.34, delays=0.1/0/0.11/0.13, dsn=2.0.0, status=sent (250 Ok 0000014b5f678593-a0e399ef-a801-4655-ad6b-19864a220f38-000000)

试图用 – echo "test" | mail -s "a test" me@noone.com -- dude@thisguy.com来改变地址 echo "test" | mail -s "a test" me@noone.com -- dude@thisguy.com

这改变了原始值,但不是从值: Feb 6 09:09:09 myserver postfix/qmgr[28875]: 6BD362269D: from=<root@myserver.com>, size=474, nrcpt=2 (queue active) Feb 6 09:09:09 myserver postfix/smtp[20505]: 6BD362269D: to=<me@noone>, orig_to=<dude@thisguy.com>, relay=myMTA[xxxx]:25, delay=0.31, delays=0.06/0/0.09/0.15, dsn=2.0.0, status=sent (250 Ok 0000014b5f6d48e2-a98b70be-fb02-44e0-8eb3-e4f5b1820265-000000)

接下来用-r和 – 来调整from和orig-to。 echo "test" | mail -s "a test" -r dude@comeguy.com me@noone.com -- dude@someguy.com

和日志: Feb 6 09:17:11 myserver postfix/qmgr[28875]: E3B972264C: from=<dude@someguy.com>, size=459, nrcpt=2 (queue active) Feb 6 09:17:11 myserver postfix/smtp[21559]: E3B972264C: to=<me@noone.com>, orig_to=<dude@someguy.com>, relay=myMTA[xxxx]:25, delay=1.1, delays=0.56/0.24/0.11/0.17, dsn=2.0.0, status=sent (250 Ok 0000014b5f74a2c0-c06709f0-4e8d-4d7e-9abf-dbcea2bee2ea-000000)

这是如何为我工作。 希望这有助于某人。

这适用于Centos7

echo "This is the main body of the mail" | mail -s "Subject of the Email" -r seneder_address@whatever.com recipent_address@example.com

在CentOs5上:-r from@me.omg

 echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -- -f from_user@example.com -F "Elvis Presley" 

要么

 echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -aFrom:"Elvis Presley<from_user@example.com>" 

感谢所有的示例提供者,有些不是。 下面是另一个简单的示例格式,为我工作。

 echo "Sample body" | mail -s "Test email" from=sender-addrs@example.com recepient-addres@example.com 

我面临同样的问题。 但我解决了这个问题只是在/ ect / passwd字段中设置geko字段。 Postfix默认发送来自用户login的邮件。 让我们假设你想从字段变成警报。 你只需要在你喜欢的任何编辑器中编辑/ etc / passwd文件。

  1. vim / ect / passwd

root:x:0:0: 警告 :/ root:/ bin / bash

  1. 重新启动后缀。

现在检查结果。

  1. 回声“这是邮件的主体”| mail -s“电子邮件主题”user@domain.com

现在在收件人。 From将显示为您在Geko字段中指定的Alerts。 希望这个解决scheme适合你。