显示名称而不是电子邮件的电子邮件标题的格式是什么?

问题描述:

我正在尝试创建一个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"; 

我得到的recieving最终的结果是:

"noreply"@rilburskryler.net rnReply到:[email protected]:PHP/5.2.13rnMIME-版本: 1.0

要有名称,而不是电子邮件地址显示,使用以下命令:

"John Smith" <[email protected]> 

容易。

关于虚线休息,那是因为你包围在单引号的文本,而不是引号:

$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); 
+7

显示名称包含空白字符时,需要引用它。 – Gumbo 2010-09-04 21:45:29

+2

@Gumbo:刚刚测试过。工作不带引号。不知道这是否是标准,或只是一个非常灵活/宽容的结构... – 2010-09-04 22:00:51

+0

我想后者;参见[RFC 822](http://tools.ietf.org/html/rfc822#section-6.1)。 – Gumbo 2010-09-04 22:23:47

$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); 

在一个single quoted string,只有转义序列\'\\通过'被替换和\。您需要使用double quotes有转义序列\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"; 

你也可以使用一个数组来收集报头字段,并把它们后来在一起:

$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);