我一直在解决一个问题,因为有很多字段的表单没有通过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;
}
}
?>
答案 0 :(得分:1)
声明变量,因为如果数组为空,则循环不会设置它们:
$availability_msg = '';
$sport_msg = '';
$email_body
中也存在语法错误,这是修复后的版本:
$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;
答案 1 :(得分:0)
您是否检查过这些值,例如
error_log(print_r($_POST,1));
error_log(print_r($_POST['availability'],1);
error_log(print_r($_POST['sport'] ,1));
您是否事先检查了传递给邮件功能的变量,例如(对所有这些变量执行此操作):
error_log(" TO>".$to."<");
此外,邮件功能的返回值是什么?