将BCC字段添加到php联系表单

时间:2013-09-29 13:50:26

标签: php contact-form

我的网站上使用以下代码通过联系表单发送电子邮件。 我想在BCC上添加一个额外的电子邮件地址,但无法弄清楚如何这样做,所以欢迎你的帮助。非常感谢

<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }

  //send email
  mail( "xyz@hhhjkh.com", "New message from: ".$_POST['name'], $_POST['message'], "From:" . $_POST['email'] );

}
?>

1 个答案:

答案 0 :(得分:0)

您只需在与BCC标题相同的参数中添加From标题:

"From:" . $_POST['email']

变为:

"From:" . $_POST['email'] . "\r\n" . "BCC:mail@test.com"

参见the PHP mail() page上的示例#4。 (或示例#2,尽管#4特别具有BCC标题。)