2012-10-23 101 views
0

可能重複:
PHP Mail, CC FieldPHP接觸形式與CC

我加入了CC,以在客戶網站上填寫表單的任務。我真正需要的是將表單提交給指定爲[email protected]的收件人,並將其抄送(碳複製)到另一個電子郵件地址。

這裏就是我有PHP明智的,在DOCTYPE上述文件的頂部(HTML 5)

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

     if(trim($_POST['contactname']) == '') { 
      $hasError = true; 
     } else { 
      $name = trim($_POST['contactname']); 
     } 

     if(trim($_POST['subject']) == '') { 
      $hasError = true; 
     } else { 
      $subject = trim($_POST['subject']); 
     } 


     if(trim($_POST['email']) == '') { 
      $hasError = true; 
     } else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { 
      $hasError = true; 
     } else { 
      $email = trim($_POST['email']); 
     } 

     if(trim($_POST['message']) == '') { 
      $hasError = true; 
     } else { 
      if(function_exists('stripslashes')) { 
       $comments = stripslashes(trim($_POST['message'])); 
      } else { 
       $comments = trim($_POST['message']); 
      } 
     } 

     if(!isset($hasError)) { 
      $emailTo = '[email protected]'; 
      $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments"; 
      $headers = 'From: Sigma Web Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

      mail($emailTo, $subject, $body, $headers); 
      $emailSent = true; 
     } 
    } 
?> 
+1

CC是一個頭。只需將它添加到你的'$ headers'字符串中即可。 –

+0

誰讓你使用'if(function_exists('stripslashes'))'?您是否錯誤地修改了不專業的magic_quotes解決方法? – mario

+0

還應該指出這是令人討厭的代碼。看看SwiftMailer等。人。如果你想這樣做的話。 – Treffynnon

回答

0

變化

$headers = 'From: Sigma Web Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

$headers = 'From: Sigma Web Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email. "\r\n"; 
$headers .= 'Cc: '.$ccemail; // $ccemail is carbon copying email address 

看到更多關於mail()

+0

感謝Prakash,工作過一種享受,但需要你的前後版本,因爲我說的不是一個真正的PHP傢伙。 – pixelator