2013-04-01 93 views
-1

我有以下代碼。請幫助我在此腳本中添加密件抄送:如何在php郵件中添加密件抄送

<?php 
if(isset($_POST['from'])) { 

$email_to = $_POST['to']; 

$email_subject = "Invitation"; 


function died($error) { 

    echo "We're sorry, but there's errors found with the form you submitted.<br /><br />"; 
    echo $error."<br /><br />"; 
    echo "Please go back and fix these errors.<br /><br />"; 
    die(); 
} 

if(!isset($_POST['name']) || 
    !isset($_POST['address']) ||   
    !isset($_POST['from']) || 
    !isset($_POST['phone']) || 
    !isset($_POST['to'])) { 
    died('We are sorry, but there appears to be a problem with the form you submitted.');  
} 

$name = $_POST['name']; 
$address = $_POST['address']; 
$email_from = $_POST['from']; 
$phone = $_POST['phone']; 
$email_to = $_POST['to']; 


$error_message = ""; 
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email_from)) { 
$error_message .= 'From Email Address you entered does not appear to be valid.<br   />'; 
} 
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$name)) { 
$error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
} 
if(!preg_match($email_exp,$email_to)) { 
$error_message .= 'TO Email Address you entered does not appear to be valid.<br />'; 
} 

if(strlen($error_message) > 0) { 
died($error_message); 

} 
$email_message = "Got one invitation request from:.\n\n"; 

function clean_string($string) { 
    $bad = array("content-type","bcc:","to:","cc:","href"); 
    return str_replace($bad,"",$string); 
} 

$email_message .= "".clean_string($name)."\n"; 
$email_message .= "".clean_string($address)."\n"; 
$email_message .= "".clean_string($phone)."\n"; 

$headers = 'From: '.$email_from."\r\n". 
Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 

echo "<script>window.close(), 2000;</script>"; 
?> 
+0

,如果你作出努力,以增加BCC,然後打開一個問題,這可能會更好,如果/當你有問題。 –

+0

你真的會更喜歡使用像SwiftMailer這樣的庫來代替'mail()'命令。 – ceejayoz

回答

2

BCC標題允許您執行此操作。你的劇本幾乎全部設置爲這個已經 - 只是改變這一點:

$headers = 'From: '.$email_from."\r\n". 
      'Reply-To: '.$email_from."\r\n" . 
      'X-Mailer: PHP/' . phpversion(); 

到:

$headers = 'From: '.$email_from."\r\n". 
      'Reply-To: '.$email_from."\r\n" . 
      'BCC: '.$email_bcc."\r\n". 
      'X-Mailer: PHP/' . phpversion(); 
相關問題