php 显示名称而不是电子邮件的电子邮件标题的格式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3644081/
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
What is the format for e-mail headers that display a name rather than the e-mail?
提问by RonLugge
I'm trying to create a php script that will handle a mailing list for me using a mySQL database, and I have most of it in place. Unfortunately, I can't seem to get the headers to work right, and I'm not sure what the problem is.
我正在尝试创建一个 php 脚本,该脚本将使用 mySQL 数据库为我处理邮件列表,并且我已经准备好了大部分内容。不幸的是,我似乎无法让标题正常工作,而且我不确定问题是什么。
$headers='From: [email protected] \r\n';
$headers.='Reply-To: [email protected]\r\n';
$headers.='X-Mailer: PHP/' . phpversion().'\r\n';
$headers.= 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1 \r\n';
$headers.= "BCC: $emailList";
The result I'm getting on the recieving end is:
我收到的结果是:
"noreply"@rilburskryler.net rnReply-To: [email protected]: PHP/5.2.13rnMIME-Version: 1.0
"noreply"@rilburskryler.net rnReply-To: [email protected]: PHP/5.2.13rnMIME-Version: 1.0
回答by Luke Stevenson
To have names, as opposed to email addresses shown, use the following:
要显示姓名,而不是显示电子邮件地址,请使用以下内容:
"John Smith" <[email protected]>
Easy.
简单。
Regarding the broken line breaks, that is because you are enclosing the text in apostrophes rather than quotation marks:
关于断行符,那是因为您将文本括在撇号而不是引号中:
$headers = array(
'From: "The Sending Name" <[email protected]>' ,
'Reply-To: "The Reply To Name" <[email protected]>' ,
'X-Mailer: PHP/' . phpversion() ,
'MIME-Version: 1.0' ,
'Content-type: text/html; charset=iso-8859-1' ,
'BCC: ' . $emailList
);
$headers = implode( "\r\n" , $headers );
回答by Gumbo
Within a single quoted string, only the escape sequences \'
and \\
are replaced by '
and \
respectively. You need to use double quotesto have the escape sequences \r
and \n
to be replaces by the corresponding characters:
在单引号字符串中,只有转义序列\'
和\\
分别由'
和替换\
。您需要使用双引号来获得转义序列\r
并被\n
相应的字符替换:
$headers = "From: [email protected] \r\n";
$headers.= "Reply-To: [email protected]\r\n";
$headers.= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers.= "BCC: $emailList";
You could also use an array to collect the header fields and put them later together:
您还可以使用数组来收集标题字段,然后将它们放在一起:
$headers = array(
'From: [email protected]',
'Reply-To: [email protected]',
'X-Mailer: PHP/' . phpversion(),
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1',
"BCC: $emailList"
);
$headers = implode("\r\n", $headers);
回答by cmptrwhz
$to = '[email protected]';
$to .=', ' . $_POST['Femail'];
$subject = 'Contact Us Form';
// message
$message ="<html>
<head>
<title>Email title</title>
</head>
<body>
<h3>important message follows</h3>
<div>
you are being brought this email to be safe.
</div>
</body>
</html>";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: SendersEmailName <[email protected]>' . "\r\n";
$headers .= 'From: YourName <[email protected]>' . "\r\n";
$headers.='X-Mailer: PHP/' . phpversion()."\r\n";
$headers.= "BCC: $emailList";
mail($to, $subject, $message, $headers);