使用 linux mail 命令自动发送邮件时如何更改发件人姓名(不是电子邮件地址)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6537297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:51:38  来源:igfitidea点击:

How to change sender name (not email address) when using the linux mail command for autosending mail?

linuxemailmail-sender

提问by therobyouknow

Mailbox shows the sender name as "Apache", because the mail I am autosending is being sent from a Perl CGI program. How do I change it to something else?

邮箱将发件人名称显示为“Apache”,因为我自动发送的邮件是从 Perl CGI 程序发送的。我如何将其更改为其他内容?

回答by sam hocevar

You just need to add a From:header. By default there is none.

您只需要添加一个From:标题。默认情况下没有。

echo "Test" | mail -a "From: Someone <[email protected]>" [email protected]

You can add any custom headers using -a:

您可以使用-a以下方法添加任何自定义标题:

echo "Test" | mail -a "From: Someone <[email protected]>" \
                   -a "Subject: This is a test" \
                   -a "X-Custom-Header: yes" [email protected]

回答by Gryphius

It depends on what sender address you are talking about. The sender address visble in the recipients mailprogramm is extracted from the "From:" Header. which can probably easily be set from your program.

这取决于您所谈论的发件人地址。收件人邮件程序中可见的发件人地址是从“发件人:”标题中提取的。这可能很容易从您的程序中设置。

If you are talking about the SMTP envelope sender address, you can pass the -f argument to the sendmail binary. Depending on the server configuration you may not be allowed to do that with the apache user.

如果您正在谈论 SMTP 信封发件人地址,您可以将 -f 参数传递给 sendmail 二进制文件。根据服务器配置,您可能不被允许使用 apache 用户执行此操作。

from the sendmail manpage:

从 sendmail联机帮助页

   -f <address>
                 This  option  sets  the  address  of the envelope sender of a
                 locally-generated message (also known as  the  return  path).
                 The  option  can normally be used only by a trusted user, but
                 untrusted_set_sender can be set to allow untrusted  users  to
                 use it. [...]

回答by user1106046

If no From: header is specified in the e-mail headers, the MTA uses the full name of the current user, in this case "Apache". You can edit full user names in /etc/passwd

如果电子邮件标头中未指定 From: 标头,则 MTA 使用当前用户的全名,在本例中为“Apache”。您可以在 /etc/passwd 中编辑完整的用户名

回答by MoSs

mail -s "$(echo -e "This is the subject\nFrom: Paula <[email protected]>\n
Reply-to: [email protected]\nContent-Type: text/html\n")" 
[email protected] < htmlFileMessage.txt

the above is my solution..just replace the "Paula" with any name you want e.g Johny Bravo..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.

以上是我的解决方案..只需将“Paula”替换为您想要的任何名称,例如 Johny Bravo.. 可以在回复之后和之前添加任何额外的标题...只需确保您之前了解您的标题语法添加它们......这对我来说非常有效。

回答by Andy

On Ubuntu 14.04 none of these suggestions worked. Postfix would override with the logged in system user as the sender. What worked was the following solution listed at this link --> Change outgoing mail address from root@servername - rackspace sendgrid postfix

在 Ubuntu 14.04 上,这些建议都不起作用。Postfix 将使用登录的系统用户作为发件人进行覆盖。有效的是此链接中列出的以下解决方案 -->从 root@servername 更改外发邮件地址 - rackspace sendgrid postfix

STEPS:

脚步:

1) Make sure this is set in /etc/postfix/main.cf:

1) 确保在 /etc/postfix/main.cf 中设置:

   smtp_generic_maps = hash:/etc/postfix/generic

2) echo 'www-data [email protected]' >> /etc/postfix/generic

2) echo 'www-data [email protected]' >> /etc/postfix/generic

3) sudo postmap /etc/postfix/generic

3) sudo postmap /etc/postfix/generic

4) sudo service postfix restart

4) sudo 服务 postfix 重启

回答by RafaSashi

You can use the "-r" option to set the sender address:

您可以使用“-r”选项来设置发件人地址:

mail -r [email protected] -s ...

In case you also want to include your real name in the from-field, you can use the following format

如果您还想在 from-field 中包含您的真实姓名,您可以使用以下格式

mail -r "[email protected] (My Name)" -s "My Subject" ...