我沒有使用 php 的經驗,由于舊的 formtomail cgi 腳本不再在主機服務器上作業,我決定切換到一個簡單的 html 到 php 郵件,用于網站上的聯系頁面。我在網上獲取了一個模板并設定了 html 頁面,但在編輯 mail.php 檔案時遇到了困難,因此我的 html 表單上的所有資料請求都會通過電子郵件發送給我。我使用的模板只有電子郵件和訊息。我的 html 聯系人頁面有不同的請求,即聯系人姓名而不是姓名,并要求提供更多資訊。所以我需要 php 表單來通過電子郵件發送所有這些資訊,并且需要有關如何編輯 php 檔案以包含 html 聯系頁面上請求的內容的幫助。
我有一個包含以下內容的聯系表單 HTML 頁面:
<form action="mail.php" method="POST">
<p class="bodytextgrey">
Contact Name:<br /><input type="text" name="contact name" size="50" /><br />
Firm Name:<br /><input type="test" name="firm name" /><br /><br />
E-mail: (Please enter the email address you would like the document is to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br /><input type="test" name="job number" /><br /><br />
Document you require:<br /><form action=""><select name="Document you require">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br /><input type="text" name="Discount code" size="20" /><br /><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
然后我有一個 mail.php (我的電子郵件是我的實際電子郵件):
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "MY EMAIL";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
還希望它能夠顯示一個現有的thank you.html 頁面,而不僅僅是白頁上的一個thankyou 詞,因此還需要幫助添加我如何鏈接它。
任何幫助,將不勝感激
uj5u.com熱心網友回復:
格式化您的代碼將極大地幫助您開始除錯。我已經用一些縮進和換行符重新格式化了您當前的 HTML,以幫助說明:
<form action="mail.php" method="POST"> <!-- missing closing tag -->
<p class="bodytextgrey"> <!-- missing closing tag -->
Contact Name:<br />
<input type="text" name="contact name" size="50" /><br />
Firm Name:<br />
<input type="test" name="firm name" /><br /><br />
E-mail: (Please enter the email address you would like the document to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br />
<input type="test" name="job number" /><br /><br />
Document you require:<br />
<form action=""> <!-- nested form in a form will break things -->
<select name="Document you require">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br />
<input type="text" name="Discount code" size="20" /><br /><br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
這有幾個問題:
<p>在任何地方都沒有</p>(結束標簽)打開<form action="">不允許嵌套在現有表單中,這會導致提交問題(請參閱https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form上的“允許的內容” )</form>存在的關閉與 匹配<form action="">,但您缺少</form>- 你在任何地方都沒有
message領域
這是帶有我建議的修復的更新后的 HTML:
<form action="mail.php" method="POST">
<p class="bodytextgrey">
Contact Name:<br />
<input type="text" name="contact_name" size="50" /><br />
Firm Name:<br />
<input type="test" name="firm_name" /><br /><br />
E-mail: (Please enter the email address you would like the document to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br />
<input type="test" name="job_number" /><br /><br />
Document you require:<br />
<select name="document">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br />
<input type="text" name="discount" size="20" /><br /><br />
Message:<br />
<textarea name="message"></textarea><br /><br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</p>
</form>
正如其他人所提到的,目前尚不清楚 500 錯誤是什么,因此需要先解決這個問題,然后才能正常作業。但這里有一個經過清理的 PHP 版本。這已經在https://3v4l.org/WePRh進行了簡單的測驗,雖然它不會顯示表單或任何輸入值,但它確實證明了腳本運行。格式更好,包括所有表單輸入,并且應該使事情更容易使用:
<?php
// get all form values
$input['Contact name'] = $_POST['contact_name'] ?? '';
$input['Firm name'] = $_POST['firm_name'] ?? '';
$input['Email'] = $_POST['email'] ?? '';
$input['Job number'] = $_POST['job_number'] ?? '';
$input['Document'] = $_POST['document'] ?? '';
$input['Discount'] = $_POST['discount'] ?? '';
$input['Message'] = $_POST['message'] ?? '';
// generate the email content (i.e. each line is like "Contact name: Mark")
$formContent = '';
foreach($input as $key => $value) {
$formContent .= $key . ": " . $value . "\n";
}
$to = "[email protected]"; // TODO: replace this with your email address
$subject = "Contact Form";
/*
Note that this was likely causing downstream issues. Most email clients (gmail, outlook) will mark emails as spam
if you try to send an email from an email address that your server isn't configured to send from.
See https://stackoverflow.com/questions/17533863/how-to-configure-php-to-send-e-mail for more info.
It's also safer to define an email address like "no-reply@example.com" to send your emails from.
*/
$mailheader = "From: [email protected]";
mail($to, $subject, $formContent, $mailheader) or die('Error sending email');
// redirect to the thank you page
// TODO: update this URL to be yours
header('Location: http://www.example.com/thankyou.html');
exit;
?>
uj5u.com熱心網友回復:
您可以使用 header() 函式:示例:
header('location: thankyou.html');
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/522408.html
標籤:phphtml电子邮件
