2010-09-04 80 views
35

我正在嘗試創建一個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

回答

88

要有名稱,而不是電子郵件地址顯示,使用以下命令:

"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

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

在一個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); 
相關問題