【问题标题】:Contact Form send via email using PHP使用 PHP 通过电子邮件发送联系表格
【发布时间】:2014-03-27 02:15:32
【问题描述】:

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

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

HTML - 页面地址:http://www.plymouthparentpartnership.org.uk/index.php?p=32_3

<span class="title">Parent Request for Involvement in Parenting Programmes</span>
<p>If you are a <span style="font-weight: bold;">Parent</span>, please complete this form to request a Parenting Programme.</p>
<p>If you are a <span style="font-weight: bold;">Professional</span>, please use the <a href="http://www.plymouthparentpartnership.org.uk/index.php?p=32_4" title="Professional Form to request Parenting Programme">Professional Form</a> to request a Parenting Programme.</p>
<div style="WIDTH: 75%" class="box-round-corner">
  <form action="parent-p-programme.php" method="post">
    <div>
      <div>
        <div>
          <table width="100%" cellspacing="0" cellpadding="2" border="0">
            <tbody>
              <tr>
                <td colspan="2"> <span class="error">* required field.</span></td>
              </tr>
              <tr>
                <td>Name:</td>
                <td>
                  <input type="text" name="name" size="35" /> <span class="error"> *</span></td>
              </tr>
              <tr>
                <td style="VERTICAL-ALIGN: top">Telephone Number or<br />
                   Mobile number if main number:</td>
                <td>
                  <input type="text" name="tel" size="35" /> <span class="error"> *</span></td>
              </tr>
              <tr>
                <td>Mobile Number:</td>
                <td>
                  <input type="text" name="mobile" size="35" /></td>
              </tr>
              <tr>
                <td>Email Address:</td>
                <td>
                  <input type="email" name="email" size="35" /></td>
              </tr>
              <tr>
                <td>Course Required:</td>
                <td>
                  <select name="course">
                    <option selected="selected">Please select course</option>
                    <option>Incredible Years</option>
                    <option>Strengthening Families 10-14 years</option>
                  </select> <span class="error"> *</span></td>
              </tr>
              <tr>
                <td style="VERTICAL-ALIGN: top">Date of Birth of<br />
                   Child / Young Person:</td>
                <td>
                  <input type="text" name="dob" size="35" /> <span class="error"> *</span></td>
              </tr>
              <tr>
                <td style="VERTICAL-ALIGN: top">Additional Information:</td>
                <td>
                  <textarea name="info" rows="5" cols="32"></textarea></td>
              </tr>
            </tbody>
          </table>
          <p>We aim to respond to your enquiry within 2 working days.</p>
          <p>
            <input type="submit" value="Send Request" />
            <input type="reset" value="Clear Form" /></p></div> </div> </div>
  </form></div>

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();
}
?>

填写完字段后弹出提交页面的“谢谢”,它似乎可以识别未填写字段的位置,但验证似乎也无法正常工作。

感谢页面提交表单后

<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>

当我在本地进行测试时,PHP 脚本没有发送任何电子邮件。整个晚上都在看它,但就是看不出我做错了什么。因此,提前感谢您提供的任何帮助或建议。

【问题讨论】:

  • 您是否尝试过打印出变量,以便查看您实际上是在传递电子邮件和其他条目?
  • 邮件服务器是否正常工作?你能用mail() 发送一封非常简单的电子邮件吗?另外,因为你只是在学习这个功能,我建议使用其他的东西,比如 PHPMailer。
  • @sue D,根据您的描述,您是 php 新手,那么为什么要使用这个功能齐全的概念,这是一种简单的形式,您也可以按照您的参考网站,
  • @Sue D,你的条件也是错误的, if ($problem && strlen($data) == 0) { show_error($problem); }
  • 考虑使用if (filter_var($email,FILTER_VALIDATE_EMAIL)) { .. }验证电子邮件

标签: php regex forms email


【解决方案1】:

php mail函数需要3个参数,你只给了2个。

http://www.php.net/manual/en/function.mail.php

我用 unix 邮件对其进行了测试,一旦应用了主题,它就可以正常工作。

【讨论】:

    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2014-08-17
    相关资源
    最近更新 更多