【问题标题】:Specify the from user when sending email using the mail command使用 mail 命令发送电子邮件时指定 from 用户
【发布时间】:2010-09-12 06:34:57
【问题描述】:

有谁知道在使用 mail 命令发送电子邮件时如何更改 from 用户?我已经浏览了手册页,但看不到如何执行此操作。

我们正在运行 Redhat Linux 5。

【问题讨论】:

  • 删除了“重复”标签。我认为不鼓励使用此标签。
  • 您可能只需键入“man mail”就可以更快地得到这个问题的答案,这将显示该命令的手册。
  • -r from@address.com - RHEL7 仅供参考

标签: linux email redhat


【解决方案1】:

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

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

【讨论】:

  • @michabbb 无法在我的 Debian Wheezy 上运行,结果:“来自:some@mail.tld:没有这样的文件或目录”
  • 你有没有像上面的例子那样把你的参数用引号括起来?
  • 是的,带有引号,例如您的示例,它显示了我写的错误。但是 mail -r 选项正在工作stackoverflow.com/questions/119390/…(顺便说一句,如果您希望人们收到有关您的回复的通知,请不要忘记提及@username)
  • 也在 ubuntu 14.04 (/etc/alternatives/mail -> /usr/bin/bsd-mailx) 上工作,但不在 16.04 (/etc/alternatives/mail -> /usr/bin/s-nail) 上工作
  • apt-get install mailutils之后在Ubuntu Xenial上工作。
【解决方案2】:

http://www.mindspill.org/962 好像有办法了。

基本上:

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

【讨论】:

  • 似乎没有通过嗅探测试。 -- 将选项结束标记为getopt(3),那么-f-- 之后如何作为选项工作?此外,在 Ubuntu 上阅读 (bsd) mail 的手册页并没有提及任何此类 -f 参数支持。最后,在 Ubuntu 14.04 上进行测试(有些怀疑和希望)表明它确实不起作用。 FWIW(再次难以置信)最初的 mindspill.org URL 说它可以在 Red Hat 上运行而没有指定版本。
  • 对详细输出 mail -v -s ... 使用 -v 选项。例如,如果由于邮件服务器配置不正确而导致邮件无法投递,smtp 命令日志将显示问题所在。
  • 在 debian 9 中 -f 用于传递文件名
【解决方案3】:

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

-r = 从地址 -R = 回复地址

作者指出他的邮件版本不支持这个标志。但是,如果您有一个可以正常工作的版本。

【讨论】:

  • 对于我们拥有的邮件版本的邮件命令选项,-r 不是有效选项。
  • 在 Debian Wheezy 上,-r 正在处理邮件命令以编辑发件人地址,但 -R 不能用于编辑回复地址。
  • 只是为了向其他人澄清 -r 确实会影响检查 spf 记录的地址
  • 在 Ubuntu 18 上与 Postfix 配合良好。
  • -r 为我工作并转到我的 gmail 帐户,而不是延迟垃圾邮件(在配置 spf/dkim 之后)但不是 -R 标志,它不是原始问题的一部分。跨度>
【解决方案4】:

通过 SMTP 发送时,mail 手册页建议设置 from 变量,以这种方式(在 CentOS 6 上测试):

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

您还可以使用-a 选项附加文件:

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

【讨论】:

  • 在 CentOS 7.5 上为我工作
【解决方案5】:

这些都不适合我(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

【讨论】:

    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

      以上都不适合我。我花了很长时间才弄清楚,希望这对下一个人有帮助。

      我正在使用带有 mailutils v2.1 的 Ubuntu 12.04 LTS。

      我在网上某处找到了这个解决方案,不知道在哪里,再也找不到了:

      -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
      

      【讨论】:

        【解决方案8】:

        这是solution

        -r 之后的第二个最简单的解决方案(即指定一个 From: 标头并用这样的换行符将其与正文分开

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

        只支持几个邮件版本,不知道redhat带什么版本)。

        PS:大多数版本的邮件都很糟糕!

        【讨论】:

          【解决方案9】:

          在 CentOs5 上:-r from@me.omg

          【讨论】:

            【解决方案10】:
            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>"
            

            【讨论】:

              【解决方案11】:

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

              如果以 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[x.x.x.x]: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

              这会更改 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[x.x.x.x]: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[x.x.x.x]: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)

              这就是它对我的工作方式。希望这对某人有所帮助。

              【讨论】:

                【解决方案12】:

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

                【讨论】:

                • -r 显示名称
                【解决方案13】:

                这是 2018 年关于 Debian 9 延伸的答案。

                注意-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
                

                希望这会有所帮助!

                【讨论】:

                  【解决方案14】:

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

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

                  【讨论】:

                    【解决方案15】:

                    感谢所有示例提供者,有些提供者有用,有些则没有。 下面是另一个对我有用的简单示例格式。

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

                    【讨论】:

                      【解决方案16】:

                      我遇到了同样的问题。但我解决了这个问题,只是在 /ect/passwd 字段中设置 geko 字段。 Postfix 默认从用户登录发送邮件。假设您想从字段更改为警报。您只需要在您喜欢的任何编辑器中编辑 /ect/passwd 文件。

                      1. vim /ect/passwd

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

                      1. 重启后缀。

                      现在检查结果。

                      1. echo "这是邮件的主体" | mail -s "邮件主题" user@domain.com

                      现在在收件人中。正如您在 geko 字段中指定的那样,发件人将显示为 Alerts。 希望此解决方案对您有用。

                      【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2017-08-05
                      • 1970-01-01
                      • 2016-04-20
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多