簡體   English   中英

使用PHP通過電子郵件發送聯系表

[英]Contact Form send via email using PHP

我是PHP的初學者,並嘗試建立聯系表單,該表單將在使用正則表達式進行驗證后將使用PHP腳本將輸入的信息發送到電子郵件地址,然后打開“謝謝”頁面。 我一直在嘗試遵循下一頁上的教程: http : //myphpform.com/php-form-tutorial.php

我已經使用Uniform Server設置了測試服務器,並使用郵件服務器正在工作的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腳本未發送任何電子郵件。 花了整整一個晚上看着它,但看不到我在做什么錯。 因此,在此先感謝您提供任何幫助或建議。

php mail函數需要3個參數,您只給了2個參數。

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

我用unix郵件對其進行了測試,一旦應用了主題,它就可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM