2014-01-29 78 views
0

我正在使用PHP联系表单从网站发送电子邮件。我在'index.html'页面中有表单标记,并使用'process.php'页面来处理脚本。以PHP电子邮件的形式发送正确的信息

下面是我使用的process.php页面的代码:

<?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: [email protected]' . "\r\n"; 
    $headers .= 'From: Website - No Reply <[email protected]>' . "\r\n" 
    $to = '[email protected]'; 
    $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; 
    } 
?> 

我的问题:我收到的形式的结果,但他们不是很清晰。如果我放置
标记,它们会形成显示html标记标记的结果。此外,我正在收到reCaptcha答案以及公钥/私钥。

有人能帮我制作清晰的邮件回复吗?

+0

什么手段 - “结果不是很清晰”? – voodoo417

+0

对不起,更具体。从此表单收到的电子邮件显示混乱的结果,没有换行符和格式不规则。 – joshSpiderb

回答

0

要删除html标签,您可以使用strip_tags函数。问题是你有“消毒”功能,但你没有使用它。 变化如下代码:

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

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

这样:

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

你的第二个问题。忽略验证码形式值改变这一行:

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

这样:

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