简体   繁体   English

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

[英]Specify the from user when sending email using the mail command

Does anyone know how to change the from user when sending email using the mail command?有谁知道如何在使用 mail 命令发送电子邮件时更改 from 用户? I have looked through the man page and can not see how to do this.我已经浏览了手册页,但看不到如何执行此操作。

We are running Redhat Linux 5.我们正在运行 Redhat Linux 5。

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

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

http://www.mindspill.org/962 seems to have a solution. http://www.mindspill.org/962似乎有一个解决方案。

Essentially:本质上:

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

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

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

The author has indicated his version of mail doesn't support this flag.作者已表示他的邮件版本不支持此标志。 But if you have a version that does this works fine.但是如果你有一个可以正常工作的版本。

When sending over SMTP, the mail man page advises to set the from variable, in this way (Tested on CentOS 6):通过 SMTP 发送时, mail手册页建议以这种方式设置from变量(在 CentOS 6 上测试):

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

You could also attach a file using the -a option:您还可以使用-a选项附加文件:

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

None of these worked for me (Ubuntu 12.04) but finally with trial & error I got:这些都不适合我(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]

(all in one line, there is no space in "-Sreplyto") (全部在一行中,“-Sreplyto”中没有空格)

I got this mail command from:我从以下地址收到此邮件命令:

apt-get install mailutils

You can append sendmail options to the end of the mail command by first adding --.您可以通过首先添加 -- 将 sendmail 选项附加到邮件命令的末尾。 -f is the command on sendmail to set the from address. -f 是 sendmail 上设置发件人地址的命令。 So you can do this:所以你可以这样做:

mail recipient@foo.com -- -f sender@bar.com邮件收件人@foo.com -- -f sender@bar.com

None of the above worked for me.以上都不适合我。 And it took me long to figure it out, hopefully this helps the next guy.我花了很长时间才弄明白,希望这能帮助下一个人。

I'm using Ubuntu 12.04 LTS with mailutils v2.1.我将 Ubuntu 12.04 LTS 与 mailutils v2.1 一起使用。

I found this solutions somewhere on the net, don't know where, can't find it again:我在网上某处找到了这个解决方案,不知道在哪里,又找不到了:

-aFrom:Servername-Server@mydomain.com

Full Command used:使用的完整命令:

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

Here's a solution .这是一个解决方案

The second easiest solution after -r (which is to specify a From: header and separate it from the body by a newline like this -r 之后的第二个最简单的解决方案(即指定 From: 标头并通过这样的换行符将其与正文分开

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

 Hi!
 .

works in only a few mail versions, don't know what version redhat carries).仅在几个邮件版本中有效,不知道redhat 带有什么版本)。

PS: Most versions of mail suck! PS:大多数版本的邮件很烂!

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

Most people need to change two values when trying to correctly forge the from address on an email.大多数人在尝试正确伪造电子邮件上的发件人地址时需要更改两个值。 First is the from address and the second is the orig-to address.第一个是起始地址,第二个是起始地址。 Many of the solutions offered online only change one of these values.许多在线提供的解决方案仅更改其中一个值。

If as root, I try a simple mail command to send myself an email it might look like this.如果作为 root,我尝试一个简单的邮件命令给自己发送一封电子邮件,它可能看起来像这样。 echo "test" | mail -s "a test" me@noone.com

And the associated logs: 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)以及相关的日志: 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)

Trying to change the from address with -- echo "test" | mail -s "a test" me@noone.com -- dude@thisguy.com尝试使用 -- echo "test" | mail -s "a test" me@noone.com -- dude@thisguy.com更改发件人地址echo "test" | mail -s "a test" me@noone.com -- dude@thisguy.com

This changes the orig-to value but not the from value: 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)这会更改 orig-to 值而不是 from 值: 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)

Next trying it with a -r and a -- to adjust the from and orig-to.接下来尝试使用 -r 和 a -- 来调整 from 和 orig-to。 echo "test" | mail -s "a test" -r dude@comeguy.com me@noone.com -- dude@someguy.com

And the logs: 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)和日志: 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)

This is how it's working for me.这就是它对我的工作方式。 Hope this helps someone.希望这可以帮助某人。

这适用于 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

Here's an answer from 2018, on Debian 9 stretch.这是 2018 年在 Debian 9 上的答案。

Note the -e for echo to allow newline characters, and -r for mailx to show a name along with an outgoing email address:请注意-e用于 echo 以允许换行符, -r用于 mailx 以显示名称以及外发电子邮件地址:

$ echo -e "testing email via yourisp.com from command line\n\nsent on: $(date)" | mailx -r "Foghorn Leghorn <sender@yourisp.com>" -s "test cli email $(date)" -- recipient@somedomain.com

Hope this helps!希望这可以帮助!

对于 CentOS,这里是工作命令:

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

在 CentOs5 上:-r from@me.omg

Thanks to all example providers, some worked for some not.感谢所有示例提供者,有些人对有些人无效。 Below is another simple example format that worked for me.下面是另一种对我有用的简单示例格式。

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

I faced the same issue.我遇到了同样的问题。 But i solved the problem just be setting geko field in /ect/passwd field.但我解决了这个问题,只是在/ect/passwd字段中设置了 geko 字段。 Postfix by default send the mail from user login. Postfix 默认从用户登录发送邮件。 Lets suppose you want to change from field to Alert.假设您想从字段更改为警报。 You just need to edit /ect/passwd file in any editor you like.您只需要在您喜欢的任何编辑器中编辑 /ect/passwd 文件。

  1. vim /ect/passwd vim /ect/passwd

root:x:0:0: Alerts :/root:/bin/bash root:x:0:0:警报:/root:/bin/bash

  1. Restart postfix.重新启动后缀。

Now check the result.现在检查结果。

  1. echo "This is the main body of the mail" | echo "这是邮件的主体" | mail -s "Subject of the Email" user@domain.com mail -s "邮件主题" user@domain.com

Now in recipient.现在在收件人。 From will be shown as Alerts as you have specified in geko field.从将显示为警告,你在GEKO领域已经确定。 Hope this solution works for you.希望这个解决方案对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用mailx发送邮件时的Output格式 - Output format when sending the mail using mailx 通过浏览器执行时,用于发送邮件的php脚本无法正常工作,但从命令行执行时,可以正常工作 - php script for sending mail wont work when executed via browser but work fine when executed from command line 从主题中的命令行发送带有bash参数的邮件 - Sending mail with bash argument from command line in subject 使用邮件命令,主题名称未显示在我的电子邮件中 - Subject Name not showing in my email using mail command 在不带-a选项的情况下,在linux下使用mail命令更改电子邮件中的“发件人”字段 - Change the From field in an email with the mail command under linux without the -a option 从PHP运行时Python不发送电子邮件 - Python is not sending email when run from PHP 使用tomcat从vmware沙箱发送电子邮件 - sending email from vmware sandbox using tomcat 从AWS Linux AMI发送时出现javax.mail.AuthenticationFailedException - javax.mail.AuthenticationFailedException when sending from AWS Linux AMI 使用php脚本从linux服务器发送邮件 - Sending mail from linux server using php script 从crontab发送邮件时,电子邮件正文内容将被删除 - email body content gets removed when mail sent from crontab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM