對於我的當地球探組來說,我和我的一個朋友正在組織一個明年的小事件。我想創建一個簡單的網頁,並在底部提供必要的信息和簡單的表單,以便他們爲活動簽名。簡單的HTML-PHP表單不會發送電子郵件
我已經有一個在家運行的webserver,運行Debian與Apache 2 PHP5。
我創建了網頁,並希望在底部添加表單。在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 = "[email protected]";
$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._%-][email protected][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 = "[email protected]";
$email_subject = "website html form submissions";
線,以配合我的電子郵件地址和期望的對象。
接下來,我保存了這兩個文件並瀏覽到我的網頁。頁面顯示正常,表單也是如此。但在填寫表格並點擊「提交」之後,至今沒有任何電子郵件將其發送到我的收件箱。
現在,我從我的服務器發送電子郵件沒有任何經驗,不知道是否有任何配置要做,或者我是否需要安裝某個程序或服務器。我可能完全錯過了一些東西,但在嘗試了幾種形式後,我發現我有點沮喪,並開始懷疑你們是否可以幫助我:-)。
親切的問候! Wouter Janssen。
刪除mail()調用中的'@'。然後測試通話的結果。它不是很有幫助,但它可能至少會給你一個線索。 _Question_你有一個郵件服務器,因爲'mail()'只將郵件從PHP傳遞到本地配置的郵件服務器。 – RiggsFolly
而不是從功能郵件「echo mail(...)」返回的「@mail(...)」結帳值。如果返回1,所有工作正常。 –
順便說一句:這是**死亡**而不是**死亡** –