【发布时间】:2015-05-20 08:32:14
【问题描述】:
我一直在对一个表单的问题进行故障排除,该表单有很多字段无法通过 PHP 邮件表单正确填充。
我回到基础,将其分解为几个字段,当第二组复选框数据添加到组合中时,我似乎遇到了问题。正如它在下面复制的那样,电子邮件将发送,并且正文包括所有内容,包括 $sport_msg。它不包括 $availability_msg 或 $message,尽管在我添加第二组复选框数据之前它已经这样做了。
我 90% 确定问题出在 $email_body 的语法上,但我不确定我做错了什么。或者它可能与两组复选框的“foreach”部分周围的括号有关。请比我更擅长 PHP 的人再给我一双眼睛吗?
我的 PHP:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$company = $_POST['company'];
$visitor_email = $_POST['email'];
$sport = $_POST['sport'];
$message = $_POST['message'];
foreach($_POST['availability'] as $value) {
$availability_msg .= "$value\n";
}
foreach($_POST['sport'] as $value) {
$sport_msg .= "$value\n";
}
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'VGFV Guide Signup';//<== Shows up in the "from" field
$email_subject = "New Guide Signup";//<== Email Subject
//Begin Email Body - I BELIEVE THIS IS WHERE MY ISSUE IS!!!
$email_body = "$name. has signed up from $company\n\n".
"Here is the info:\n \n".
"Guide Services Offered: \n".$sport_msg;
"Availability: \n".$availability_msg;
"$message \n \n".
$to = "myemailaddress@mydomain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
【问题讨论】: