如何通过 PHP 设置电子邮件发件人的姓名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9488008/
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
How do I set the name of an email sender via PHP
提问by algorithmicCoder
So I want the from
field when an email is opened to be something like
所以我希望from
打开电子邮件时的字段类似于
"Hyman Sparrow Via somesite"
as opposed to an explicit email address.
"Hyman Sparrow Via somesite"
与明确的电子邮件地址相反。
I wondering how to set this in PHP's mail()
function?
我想知道如何在 PHP 的mail()
函数中设置它?
回答by enderskill
You can accomplish this by using basic headers.
您可以通过使用基本标头来完成此操作。
<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Hyman Sparrow <[email protected]>' . PHP_EOL .
'Reply-To: Hyman Sparrow <[email protected]>' . PHP_EOL .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
回答by James
You have to set your headers:
你必须设置你的标题:
$to = "[email protected]";
$header = "FROM: Hyman Sparrow <[email protected]>\r\n";
$message = "Your message here.";
$subject = "Your subject";
mail($to,$subject,$message,$header) or die();
回答by vinoth
just use like this
就这样使用
$headers .= 'From: [Name of the person]'.'<'. $this->from .'>'. "\n";
in Header Section.
在标题部分。
回答by JWC May
If you use PHP Mailer from GitHub, then you do it by:
如果您使用来自 GitHub 的 PHP Mailer,那么您可以通过以下方式进行:
$mail->SetFrom("[email protected]", "MIBC");
回答by Luis Cardoso
$to = "[email protected]";
$message = "Your message here.";
$subject = "Your subject";
mail($to,$subject,$message,$header, '-f [email protected] -F "Hyman Sparrow"') or die();
Just add the -f parameter to provide the sender's email and -F parameter to provide the sender's name to the mail(), all as a string.
只需添加 -f 参数以提供发件人的电子邮件和 -F 参数以将发件人的姓名提供给 mail(),所有这些都是字符串。
Note:double quotes around "Hyman Sparrow"
注意:“Hyman Sparrow”周围的双引号
Assuming that you are using sendmail on a Linux machine:
假设您在 Linux 机器上使用 sendmail:
You can find more detailed information by typing "man sendmail" in your shell.
您可以通过在 shell 中键入“man sendmail”来找到更多详细信息。
回答by kwelch
I am not sure exactly what you mean, but as you can see on PHP.net mail function.
我不确定你的意思,但正如你在PHP.net mail function上看到的那样。
To add the name to the from section you need to send this in the headers.
From:Name<[email protected]>
要将名称添加到 from 部分,您需要在标题中发送它。
From:Name<[email protected]>