如何在php邮件中添加密件抄送

时间:2013-04-01 17:32:45

标签: php phpmailer

我有以下代码。请帮我在这个脚本中添加密件抄送:

<?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._%-]+@[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>";
?>

1 个答案:

答案 0 :(得分: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();