2014-01-19 50 views
0

我想編輯這個腳本在密件副本發送給自己:PHP的mail BCC不工作

$to = $your_email; 
$from = "Server Xt<[email protected]>"; 
$subject = "User Sent Msg :: $msg"; 
$HTMLmessage = $message; 

emailHTML($to, $from, $subject, $HTMLmessage); 

function emailHTML($to, $from, $subject, $HTMLmessage){ 

    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    $headers = "From: ".$from;  
    $headers .= 
    "\nMIME-Version: 1.0\n" . 
    "Content-Type: multipart/mixed;\n" . 
    " boundary=\"{$mime_boundary}\""; 

    $content .= 
    "This is a multi-part message in MIME format.\n\n" . 
    "--{$mime_boundary}\n" . 
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . 
    $HTMLmessage . "\n\n"; 

    $ok = @mail($to, $subject, $content, $headers); 

    if(!$ok) {  
    die("Error sending email"); 
    } 
} 

我已嘗試添加該$headers .= "Bcc:[email protected]"\n";但它不發送電子郵件......怎麼辦我開始修改這個腳本以使其工作?

+2

最簡單的方法:溝渠它,並使用PHPMailer或Swiftmailer。都可以在單行代碼中添加密件抄送,並在出現問題時提供更好的診斷。同樣,你永遠不要使用'@'抑制操作符。它的編碼相當於把你的手指塞進你的耳朵,並且「lalalalala聽不到你」 –

+0

你應該使用更加結構化的方式來構建你的頭字段,比如'$ headers = array('...','...'); $ headers = implode(「\ r \ n」,$ headers);'。這樣做可以消除在「Content-Type」和「BCC」之間缺少換行符的情況。 – Gumbo

+0

您的多部分郵件也是無效的,因爲它缺少[最終邊界分隔符](http://tools.ietf.org/html/rfc2046#page-20)。 – Gumbo

回答

2

單獨報頭。

function emailHTML($to, $from, $subject, $HTMLmessage) { 

    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    $headers = "From: ".$from . "\r\n"; 
    $headers .= "Bcc: [email protected]\r\n"; 
    $headers .= 
    "MIME-Version: 1.0\r\n" . 
    "Content-Type: multipart/mixed;\r\n" . 
    " boundary=\"{$mime_boundary}\""; 

    $content .= 
    "This is a multi-part message in MIME format.\r\n\r\n" . 
    "--{$mime_boundary}\r\n" . 
    "Content-Type:text/html; charset=\"iso-8859-1\"\r\n" . 
    "Content-Transfer-Encoding: 7bit\r\n\r\n" . 
    $HTMLmessage . "\r\n\r\n"; 

    $ok = @mail($to, $subject, $content, $headers); 

    if(!$ok) {  
    die("Error sending email"); 
    } 
} 
0

$headers .= "Bcc:[email protected]"\n"您正在使用的確切語法?

如果您的PHP語法無效,您應該會收到錯誤消息。

嘗試通過\r\n改變爲類似$headers .= 'Bcc:[email protected]' . "\r\n";

0

它看起來頭的順序是重要的!

$from = "Sender Name<[email protected]>"; 
$to="[email protected]"; 
$headers = "From: $from\r\n"; 
$headers .= "To: $to\r\n"; 
$headers .= "Return-Path: <".$to.">\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Bcc:[email protected]\r\n"; 
$headers .= "Content-Type: text/HTML; charset=ISO-8859-1\r\n";