简体   繁体   中英

Send correct info in php email form

I'm using a php contact form to send emails from a website. I have the form markup in an 'index.html' page and using a 'process.php' page to handle script.

Below is the code I am using in the process.php page:

<?php
 require_once('recaptchalib.php');
   $privatekey = "my key";
   $resp = recaptcha_check_answer ($privatekey,
                            $_SERVER["REMOTE_ADDR"],
                            $_POST["recaptcha_challenge_field"],
                            $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {

      die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
     "(reCAPTCHA said: " . $resp->error . ")");
   } else {
  header('Location: http://www.mydomain.com/thankyou.html');
  }

 function sendemail($data)
 {
     $data["FromIpAddress"] = $_SERVER['REMOTE_ADDR'];

     $headers  = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
     // $headers .= 'To: myemail@mydomain.com' . "\r\n";
     $headers .= 'From: Website - No Reply <no-reply@mydomain.com>' . "\r\n"
     $to  = 'myemail@mydomain.com';
     $info = array();
     foreach($data as $key => $value):
         if(strpos($key, 'cf-') === false):
             $regex = '/(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/';
             $string = preg_replace( $regex, ' $1', $key );

             $info[] = sprintf(' %s:  %s,    ', $string, $value);
         endif;
     endforeach;
     mail($to, 'New Lead from mydomain.com contact form', implode(" ", $info), $emailbody, $headers);
 }

 if(isset($_POST['cf-smartsc']) && $_POST['cf-smartsc'] == 'cf'):
     sendemail($_POST); 
     header('Location: http://www.mydomain.com/thank-you.html');    
 endif;

  function sanitize($data){
         $data= htmlentities(strip_tags(trim($data)));
         $info = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
                    '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
                     '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
                     '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments              including CDATA
     ); 
    $data = preg_replace($info, '', $data); 
         return $data;
     }
 ?>

My problem: I'm receiving the form results, but they're not very legible. If I place
tags, they form results show the html markup tags. Also, I'm receiving the reCaptcha answer along with the public/private keys.

Can someone help me produce a legible email response?

to remove html tags you can use strip_tags function. the problem is you have "sanitize" function which does that, but you are not using it. change below code:

$string = preg_replace( $regex, ' $1', $key );

$info[] = sprintf(' %s:  %s,    ', $string, $value);

to this:

$string = preg_replace( $regex, ' $1', $key );
$value = sanitize($value);
$info[] = sprintf(' %s:  %s,    ', $string, $value);

for your second problem. to ignore captcha form values change this line:

if(strpos($key, 'cf-') === false):

to this:

if(strpos($key, 'cf-') === false && strpos($key, 'recaptcha') === false):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM