我正在使用php联系表单从网站发送电子邮件。我在'index.html'页面中有表单标记,并使用'process.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: 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;
}
?>
我的问题:我收到了表单结果,但它们不是很清晰。如果我放置
标签,他们形成的结果显示html标记标签。另外,我收到了reCaptcha答案以及公钥/私钥。
有人可以帮我制作清晰的电子邮件回复吗?
答案 0 :(得分: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):