简单的HTML-PHP表单不会发送电子邮件

时间:2015-10-22 10:47:44

标签: php html forms email

对于我当地的童子军小组,我和我的一个朋友正在组织明年的小活动。我想创建一个简单的网页,其中包含必要的信息和底部的简单表格,以便他们为自己的活动签名。

我家里已经有一个正在运行的网络服务器,用Apache 2和PHP5运行Debian。

我创建了网页,并希望在底部添加表单。在http://www.freecontactform.com/html_form.php,我找到了一个表格,为我打勾。我按照那里的说明,将我的html网页中的以下部分粘贴在底部,但标记上方:

<form name="htmlform" method="post" action="html_form_send.php">
<table width="450px">
</tr>
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>

<tr>
 <td valign="top"">
  <label for="last_name">Last Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>

</tr>
<tr>
 <td valign="top">
  <label for="telephone">Telephone Number</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="comments">Comments *</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 </td>

</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">   ( <a href="http://www.freecontactform.com/html_form.php">HTML Form</a> )
 </td>
</tr>
</table>
</form>

接下来,我创建了一个名为html_form_send.php的php文件,并在其中粘贴了以下代码:

        <?php
if(isset($_POST['email'])) {
     
    // CHANGE THE TWO LINES BELOW
    $email_to = "you@yourdomain.com";
     
    $email_subject = "website html form submissions";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
     
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
 
<!-- place your own success html below -->
 
Thank you for contacting us. We will be in touch with you very soon.
 
<?php
}
die();
?>

在该文件中,我更改了

// CHANGE THE TWO LINES BELOW
$email_to = "you@yourdomain.com";
     
$email_subject = "website html form submissions";

行,以匹配我的电子邮件地址和所需主题。

接下来,我保存了两个文件并浏览到我的网页。该页面显示正常,表单也是如此。但是在填写表格并点击“提交”后,目前为止没有电子邮件进入我的收件箱。

现在,我没有任何从我的服务器发送电子邮件的经验,也不知道是否有任何配置要做,或者我是否必须安装某些程序或服务器。我可能完全错过了一些东西,但在尝试了几种形式之后我发现我有点沮丧并开始怀疑你们是否可以帮助我: - )。

亲切的问候! Wouter Janssen。

1 个答案:

答案 0 :(得分:-1)

  

我家里已经有一个正在运行的网络服务器,用Apache运行Debian   2 en PHP5。

您在谈论本地网络服务器吗?像Xampp,Wamp等?如果是,您必须配置其邮件&#34;模块&#34;。您可以通过Google搜索找到该怎么做。