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

104
使用Unix命令行发送邮件mail TO_ADDR结果为来自$USER@$HOSTNAME的邮件。有没有一种方法可以更改mail插入的“发件人:”地址?
记录一下,我在Ubuntu上使用GNU Mailutils 1.1/1.2(但我在Fedora和RHEL上看到了相同的行为)。
$ mail -s Testing chris@example.org                                                                  
Cc: 
From: foo@bar.org
Testing .
结果为:
主题:测试
收件人:<chris@example.org>
X-Mailer:mail(GNU Mailutils 1.1)
Message-Id:<E1KdTJj-00025z-RK@localhost>
发件人:<chris@localhost>
日期:2023年3月11日 星期六 下午9:28:13
From: foo@bar.org
测试
“From: foo@bar.org”行是消息正文的一部分,而不是标题的一部分。

1
这是哪种Unix版本?以及使用的邮件版本是什么?只是想知道哪里出了问题。 - Vinko Vrsalovic
哦,但是抱歉,我在运行GenToo。 - Thomas Kammeyer
man mail-r address设置发件人地址。覆盖环境或启动文件中指定的任何发件人变量。 - Kellen Stuart
mail这样的古老命令的问题在于每个版本都有不同的API。不同的实现可能会基于环境变量$USER$HOSTNAME$EMAIL设置From,或者期望您使用-f-F-a-r标志。当然,每个标志都与其他某个版本不兼容。例如,-a对于一个实现意味着添加一个新的标题,而对于另一个实现则意味着添加一个MIME附件文件。POSIX仅定义了mailx和仅限于-s标志:https://pubs.opengroup.org/onlinepubs/9699919799/utilities/mailx.html - Mikko Rantalainen
如果你想使用GNU Mailutils,那么你应该写类似于mail.mailutils -a "From: user@domain.com" -s "Subject line here" "receiver@example.com"这样的代码,而不是使用可能具有任意API的mail - Mikko Rantalainen
显示剩余3条评论
20个回答

3
在CentOS上,对我来说这个方法有效:
echo "email body" | mail -s "Subject here" -r from_email_address email_address_to

3
在Debian 7上,我仍然无法使用这个问题的答案正确设置发件人地址(始终是服务器的主机名),但我通过以下方式解决了这个问题。
安装heirloom-mailx。
apt-get install heirloom-mailx

确保它是默认选项。

update-alternatives --config mailx

撰写一条消息。

mail -s "Testing from & replyto" -r "sender <sender@example.com>" -S replyto="sender@example.com" recipient@example.net < <(echo "Test message")

这个 update-alternatives 命令是做什么的?它给了我3个选择,让我感到困惑。 - Stephane

1

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

这个命令可以在OpenBSD中使用。


1

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

-S选项可以让您指定许多字符串选项,这是修改标头等的最简单方法。


0

如何在具有UTF-8编码和文件附件的Ubuntu 16.04上设置自定义回复地址:

安装邮件客户端:

sudo apt-get install heirloom-mailx

编辑SMTP配置:

sudo vim /etc/ssmtp/ssmtp.conf
mailhub=smtp.gmail.com:587
FromLineOverride=YES
AuthUser=???@gmail.com
AuthPass=???
UseSTARTTLS=YES

发送邮件:
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)

0

这对我有用

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

0
在最新版本的GNU mailutils mail中,它只是mail -r foo@bar.com
查看原始发送的邮件,似乎设置了Return-Path: <foo@bar.com>From: foo@bar.com

0
在CentOS 5.5上,我发现设置默认域名最简单的方法是修改hosts文件。如果您的hosts文件包含WAN/公共IP地址,只需修改其中列出的第一个主机名即可。例如,您的hosts文件可能如下所示:

...
11.22.33.44 localhost default-domain whatever-else.com
...

要从whatever-else.com发送,请将其列为第一个,例如:

...
11.22.33.44 whatever-else.com localhost default-domain
...

我不能代表其他发行版(甚至是CentOS的其他版本)说话,但在我的特定情况下,以上方法完美地运作。

0

以上的解决方案都对我无效...

#!/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"

-1
之前提供的答案在CentOS5上对我无效。我安装了mutt。它有很多选项。使用mutt,您可以这样做:
export EMAIL=myfrom@example.com
export REPLYTO=myreplyto@example.com
mutt -s Testing chris@example.org

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接