使用PHP通过电子邮件发送联系人表单

使用PHP通过电子邮件发送联系人表单,php,regex,forms,email,Php,Regex,Forms,Email,我是一名PHP初学者,尝试建立一个联系表单,在使用正则表达式验证后,使用PHP脚本将输入的信息发送到电子邮件地址,然后打开一个“谢谢”页面。我一直在尝试遵循以下页面上的教程: 我已经使用UniformServer设置了一个测试服务器,并使用邮件服务器正在工作的mailtest.php文件建立了测试服务器。但是,尽管我可能会尝试,我不能让我的PHP脚本与我的表单一起工作,所以我非常感谢任何人能提供的任何建议 HTML-页面位于: 家长参与育儿计划的请求 如果您是家长,请填写此表格以申请家长教育计划

我是一名PHP初学者,尝试建立一个联系表单,在使用正则表达式验证后,使用PHP脚本将输入的信息发送到电子邮件地址,然后打开一个“谢谢”页面。我一直在尝试遵循以下页面上的教程:

我已经使用UniformServer设置了一个测试服务器,并使用邮件服务器正在工作的mailtest.php文件建立了测试服务器。但是,尽管我可能会尝试,我不能让我的PHP脚本与我的表单一起工作,所以我非常感谢任何人能提供的任何建议

HTML-页面位于:

家长参与育儿计划的请求
如果您是家长,请填写此表格以申请家长教育计划

如果您是专业人士,请使用申请家长教育计划

*必填字段。 姓名: * 电话号码或
手机号码,如果主号码: * 手机号码: 电邮地址: 所需课程: 请选择课程 难以置信的岁月 加强家庭10-14年 * 出生日期
儿童/青年: * 其他信息: 我们的目标是在2个工作日内回复您的询问

PHP脚本

<?php
/* Parent request for Parenting Programme */

/* Set e-mail recipient */
$myemail  = "myemailaddress@gmail.com"; // dummy email


/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name"); // required
$tel = check_input($_POST['tel'], "Enter your telephone number or mobile number if this is your main number"); // required
$mobile = check_input($_POST['mobile']);
$email = check_input($_POST['email']);
$course = check_input($_POST['course'], "Select course required"); // required
$dob = check_input($_POST['dob'], "Enter young persons date of birth"); // required
$info = check_input($_POST['info']);

/* If tel no is not valid show error message */
if (!preg_match("/([0-9 ]{6})?([0-9]{6})/", $tel))
{
    $tel = '';
}

/* Validate mobile no (optional) */
if (!preg_match("/([0-9 ]{6})?([0-9]{6})/", $mobile))
{
    $mobile = '';
}

/* If e-mail is not valid show error message (optional) */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    $email = '';
}

/* Email Message */
$message = "Request for $course

Name: $name
Tel No: $tel
Mobile: $mobile
Email: $email

Course: $course
Young Person's Date of Birth: $dob

Additional information: 
$info

End of message
";

/* Send message using mail () function */
mail($myemail, $message);

/* Redirect to thank you page */
header('Location: 32_5.html');
exit();

?>


<?php
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

php邮件函数需要3个参数,您只给了它2个


我用unix mail对它进行了测试,一旦应用了一个主题,它就工作得很好

你有没有试着打印出这些变量,这样你就可以看到你实际上正在传递一封电子邮件和其他条目?邮件服务器是否正常工作?你能用
mail()
发送一封非常简单的电子邮件吗?另外,由于您只是在学习这个函数,我建议您使用其他功能,如PHPMailer@sue D,正如您所描述的,您是php新手,那么您为什么要使用这个全功能的概念,这是一个简单的表单,您也可以根据您的参考站点将其简化为一个简单的表单,@sue D,您的条件也是错误的,如果($problem&&strlen($data)==0){show_error($problem)}考虑使用
if(filter_var($email,filter_VALIDATE_email)){..}
<?php
/* Parent request for Parenting Programme */

/* Set e-mail recipient */
$myemail  = "myemailaddress@gmail.com"; // dummy email


/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name"); // required
$tel = check_input($_POST['tel'], "Enter your telephone number or mobile number if this is your main number"); // required
$mobile = check_input($_POST['mobile']);
$email = check_input($_POST['email']);
$course = check_input($_POST['course'], "Select course required"); // required
$dob = check_input($_POST['dob'], "Enter young persons date of birth"); // required
$info = check_input($_POST['info']);

/* If tel no is not valid show error message */
if (!preg_match("/([0-9 ]{6})?([0-9]{6})/", $tel))
{
    $tel = '';
}

/* Validate mobile no (optional) */
if (!preg_match("/([0-9 ]{6})?([0-9]{6})/", $mobile))
{
    $mobile = '';
}

/* If e-mail is not valid show error message (optional) */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    $email = '';
}

/* Email Message */
$message = "Request for $course

Name: $name
Tel No: $tel
Mobile: $mobile
Email: $email

Course: $course
Young Person's Date of Birth: $dob

Additional information: 
$info

End of message
";

/* Send message using mail () function */
mail($myemail, $message);

/* Redirect to thank you page */
header('Location: 32_5.html');
exit();

?>


<?php
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>
<span class="title">Parenting Programme Request</span>
<p>Your request for a Parenting Programme was sent.</p>
<p>We aim to respond to your request within 2 working days.</p>