[英]Contact Form Posting
我為我的聯系表創建了 html、php 和 js 文件。 我將它上傳到我的托管服務提供商只是為了測試,因為我沒有實時服務器。 當我填寫所有必填字段並點擊提交時,我沒有收到發送到 email 地址的信息。 這只是一個簡單的表格,以便客戶可以向我發送他們的查詢和詳細信息。
$(function() { $(".form-control").on('focus', function() { $(this).parents(".form-group").addClass('focused'); }); $(".form-control").on('blur', function() { $(this).parents(".form-group").removeClass('focused'); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <div class="container"> <form action="form-handler.php" method="POST" class="form"> <div class="form-group"> <label for="name" class="form-label">Your Name</label> <input type="text" class="form-control" id="name" name="Name" placeholder="Jane Doe" tabindex="1" required> </div> <div class="form-group"> <label for="email" class="form-label">Your Email</label> <input type="text" class="form-control" id="email" name="Email" placeholder="Jane@Doe.com" tabindex="2" required> </div> <div class="form-group"> <label for="subject" class="form-label">Subject</label> <input type="text" class="form-control" id="subject" name="Subject" placeholder="Subject" tabindex="3" required> </div> <div class="form-group"> <label for="message" class="form-label">Message</label> <textarea class="form-control" name="message" id="message" cols="50" rows="5" placeholder="Write your message here..." tabindex="4"></textarea> </div> <div> <button class="btn" type="submit">Send Message</button> </div> </form> </div> </body> </html>
PHP
<?php
$message_sent = false;
if (isset($_POST['email']) && $_POST['email'] != '')
{
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
//submit the form
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$messageSubject = $_POST['subject'];
$message = $_POST['message'];
$to = "...@....com";
$body = "";
$body = "From:" . $userName . "\r\n";
$body = "Email:" . $userEmail . "\r\n";
$body = "Message:" . $message . "\r\n";
mail($to, $messageSubject, $body);
$message_sent = true;
}
}
?>
你的代碼是
$body = "From:" . $userName . "\r\n";
$body = "Email:" . $userEmail . "\r\n";
$body = "Message:" . $message . "\r\n";
mail($to, $messageSubject, $body);
從文檔中,我看到發送 FROM 等附加標頭的正確方法如下:
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
另一種更完整的方式如下:
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: Mary <mary@example.com>, Kelly <kelly@example.com>';
$headers[] = 'From: Birthday Reminder <birthday@example.com>';
$headers[] = 'Cc: birthdayarchive@example.com';
$headers[] = 'Bcc: birthdaycheck@example.com';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
最后,.. 而不是這樣的代碼:
mail($to, $messageSubject, $body);
$message_sent = true;
你應該編碼這個
$message_sent = mail($to, $messageSubject, $body);
因為您的代碼假設一切正常,但如果郵件發送與否,郵件 function 已經返回 true 或 false。
但我看到的最重要的東西是你的表單輸入名為:
和您的 php 代碼檢查
所以,... if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))應該總是返回 false,因為$_POST['email']
email 不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.