Add BCC field to a php contact form

大城市里の小女人 提交于 2019-12-12 02:29:14

问题


the code below is used on my site to send e-mails via a contact form. I'd like to add an additional e-mail address on BCC but can't figure out how to to this so your help would be welcome. Many thanks

<?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:


You can just add a BCC header in the same parameter as the From header:

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

becomes:

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

See Example #4 on the PHP mail() page. (Or Example #2, though #4 specifically has a BCC header.)



来源:https://stackoverflow.com/questions/19078798/add-bcc-field-to-a-php-contact-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!