我在我的個人Ubuntu LAMP服務器上託管我的網站。一切正常,但聯繫表格。我有Postfix配置爲通過我的Gmail郵件轉發電子郵件,它的工作出色。我知道我的服務器正在發送電子郵件。我用下面的代碼驗證了這幾次以各種地址:如何讓我的聯繫表格正確調用PHP文件?
echo "Test mail from postfix" | mail -s "Test Postfix" [email protected]
我也驗證了PHP是通過創建一個小的PHP文件「test.php的」工作並將其保存在我的/ var/www目錄。然後我通過在瀏覽器中輸入http://www.mywebsite.com/test.php來調用該文件。它始終工作,無論發送地址。
因此,知道服務器發送電子郵件,並且PHP正在運行,這導致我的代碼或與聯繫表單上單擊發送按鈕時調用PHP文件相關的一些問題。
這裏是接觸形式的html代碼:
<form method="post" name="contact" action="mail.php">
<input type="hidden" name="post" value="Send" />
<label for="name">Name:</label> <input type="text" id="name" name="name" class="required input_field" />
<label for="email">Email Address:</label> <input type="text" id="email" name="email" class="validate-email required input_field" />
<label for="url">Phone:</label> <input type="text" name="url" id="url" class="input_field" />
<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
<input style="font-weight: bold;" type="submit" class="submit_btn" name="submit" id="submit" value="Send" />
<input style="font-weight: bold;" type="reset" class="submit_btn" name="reset" id="reset" value="Reset" />
</form>
顯然,這種代碼設置一個頁面內,並匹配CSS。
這裏是PHP文件「mail.php」:
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "[email protected]";
$email_subject = "OYH Tech Website Email";
function died($error) {
// error code
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['author']) ||
!isset($_POST['email']) ||
!isset($_POST['url']) ||
!isset($_POST['text'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['author']; // required
$email_from = $_POST['email']; // required
$url = $_POST['url']; // not required
$text = $_POST['text']; // 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,$name)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($text) < 2) {
$error_message .= 'The message you entered does 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 .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($url)."\n";
$email_message .= "Message: ".clean_string($text)."\n";
// email headers
$headers = 'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
當我點擊發送它加載一個空白頁的接觸形式,僅此而已。沒有錯誤代碼或任何類型的消息,更重要的是沒有發送電子郵件。某些瀏覽器發出以下消息:網站在檢索http://www.mywebsite.com/mail.php時遇到錯誤可能是由於維護或配置不正確而導致的。
要清楚的是,我只是希望這個聯繫表格發送電子郵件到我的Gmail地址,沒有更多。我沒有收到此服務器上的電子郵件。
任何幫助將不勝感激。我一直在爲此持續工作4天。
Ughhh,我希望3天前見過那個愚蠢的大括號。那是整個球賽。非常感謝你!它最後的作品!我覺得很愚蠢!關於代碼清理,我相信你知道,這是一個學習過程。在我的防守中,它看起來比前幾天更好。我會繼續努力的:〜) – Mojoman317717