[英]Beginner PHP Mail Function Issue + Server Error
因此,我是PHP的超級初學者,但我目前正在建立我的第一個網站,我想在該網站上創建一個聯系表單,供用戶填寫,然后將其信息發送到電子郵件中。
因此,我使用“ mail()”函數為此編寫了PHP,但是由於某種原因,每當用戶單擊“提交”時,它只會轉到帶有代碼的頁面,而不是我在echo語句中鏈接到的頁面。
這是我的PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Phone: $phone \n Message: $message";
$recipient = "rowan.krishnan@tufts.edu";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$url = 'contact.html';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'
?>
這是我的html:
<div id="emailform">
<form action="mail.php" method="POST">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
Phone: <input type="text" name="phone">
Message: <textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
</div>
我的最后一個問題與我使用的Web服務器/主機(hostgator)有關。 出於某種原因,每當我上載任何包含PHP的文檔時,都會收到500 Internal Server Error,並且我不確定該怎么回事。 如果你們中的任何人知道我在說什么,那么如果您有建議,那將是很好的。
非常感謝您閱讀所有這些內容並提供幫助。
你想念一個;
回聲結束時很可能導致白頁
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
^
嘗試檢查您的錯誤日志,以查看您遇到的錯誤並嘗試打開錯誤消息
郵件的每一行也必須有\\ r \\ n
$formcontent="From: $name \r\n Phone: $phone \r\n Message: $message";
您無法在本地運行php,首先需要運行已安裝php的網絡服務器。 嘗試使用xamp,因為它確實很容易安裝和運行,並允許您在本地運行php。 http://www.apachefriends.org/en/xampp.html
生成變量后,一個簡單的PHP電子郵件腳本將如下所示:(您似乎正確)
//Generate the email
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: '. $senders_email;
mail($to, $subject, $message, $headers);
正如Kai Qing所述,請使用header('location: contact.html');
重定向頁面。 無論發送什么內容,echo都只會打印到屏幕上。 更好的方法是在HTML文檔中使用回調函數。 因此,例如,您將使用javascript函數發送表單,一旦收到響應(發送您的電子郵件),則回調函數將執行您喜歡的操作,例如顯示不同的頁面或打印確認消息。
jQuery方法看起來像這樣
//send form to order.php for further processing
var formData = $('#promo-form').serialize();
$.post('order.php',formData,formSent);
//formSent is the callback funciton
您的服務器500錯誤可能是由於在php文件上設置的權限所致。 在您的ftp客戶端中,檢查php文件的權限是否設置為0644。有關此錯誤的更多信息,請參見:
希望這可以幫助..
試試看:
<?php
//
///////////////////////////////////////////////////////////////////////
// This would be your mail.php file
///////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////
// Lets Just Check Basic PHP Installation Is Working
///////////////////////////////////////////////////////////////////////
//
if (!function_exists('mail')) {
die('mail() is not available');
}
//
//
//
///////////////////////////////////////////////////////////////////////
// Validate Required Passed Fields
///////////////////////////////////////////////////////////////////////
//
if ( isset($_POST['name']) && $_POST['name'] !='' && isset($_POST['email']) && $_POST['email'] !='' && isset($_POST['phone']) && $_POST['phone'] !='' && isset($_POST['message']) && $_POST['message'] !='' ) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
}
else
{
die('Sadly you did not complete all of the fields on our contact form');
}
//
//
///////////////////////////////////////////////////////////////////////
// Validate The Email... This Is Just An Example
///////////////////////////////////////////////////////////////////////
//
if (preg_match('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i', trim($email))) {
die('Email Address Invalid. Please Check Your Email.');
}
//
//
///////////////////////////////////////////////////////////////////////
// Build Your Email Message
///////////////////////////////////////////////////////////////////////
//
$EmailMessage .= "Name: " . $name . " \n\n";
$EmailMessage .= "Email: " . $email . " \n\n";
$EmailMessage .= "Phone: " . $phone . " \n\n";
$EmailMessage .= "Message: " . $message . " \n\n";
//
//
///////////////////////////////////////////////////////////////////////
// Build Email Header
///////////////////////////////////////////////////////////////////////
//
$EmailHeader .= "From: " . $name . " <" . $email . "> \n";
$EmailHeader .= "Reply-To: " . $name . " <" . $email . "> \n";
$EmailHeader .= "Return-Path: " . $name . " <" . $email . "> \n"; // running a windows server??
//
//
///////////////////////////////////////////////////////////////////////
// Send The Email & Confirm It Processed
///////////////////////////////////////////////////////////////////////
//
if (mail('Rowan Krishnan <rowan.krishnan@tufts.edu>', 'Contact Form', $EmailMessage, $EmailHeader)) {
//
// it appears to have worked, so redirect somewhere else
//
header("Location: contact.php");
die('Contact Form Redirect');
} else {
echo('Contact Form Failed');
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.