有没有办法可以验证我的PHP脚本是否可以将表单发送到电子邮件?我尝试在localhost中运行php网站并测试提交邮件,但是当我查看我的电子邮件时,没有发送任何内容。如果收到我的表格怎么办?当您通过电子邮件从php表单发送消息时,它是否立即收到?我怎么知道电子邮件脚本是错还是只是没发送?继承人我的HTML:
<div class="container">
<div class="row">
<div class="inquiry2 col-xs-12 col-sm-12 col-md-12 col-lg-12">
<br></br>
<br></br>
<h1>Message Us</h1>
<form action="send.php" id="frmBox" method="post" onsubmit="return formSubmit();">
<div class="form-group">
<label for="InputName">Name*</label>
<input class="form-control" type="text" placeholder="Name" name="customer_name" id="CustomerName">
</div>
<div class="form-group">
<label for="InputEmail">Email Address*</label>
<input class="form-control" type="text" placeholder="email" name="customer_email" id="CustomerEmail">
</div>
<div class="form-group">
<label for="InputPhone">Phone*</label>
<input class="form-control" type="text" placeholder="phone" name="customer_phone" id="CustomerPhone">
</div>
<div class="form-group">
<label for="InputMessage">Message*</label>
<textarea class="form-control" type="text" placeholder="" name="customer_message" id="CustomerMessage"></textarea>
</div>
<input class="btn btn-default" type="submit" value="submit">
</form>
</div>
</div>
</div>
<!--SCRIPT-->
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
function formSubmit(){
$.ajax({
type:'POST',
url:'send.php',
data:$('#frmBox').serialize(),
success:function(response){
$('#success').html(response);
}
});
var form=document.getElementById('frmBox').reset();
return false;
}
</script>
继续发送php:
<?php
$customer_name_error = $customer_email_error = $customer_phone_error = "";
$customer_name = $customer_email = $customer_phone = $customer_message= $success = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["customer_name"])) {
$customer_name_error = "Name is required";
} else {
$customer_name = test_input($_POST["customer_name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$customer_name_)) {
$customer_name_error = "Only letters and white space allowed";
}
}
if (empty($_POST["customer_email"])) {
$customer_email_error = "Email is required";
} else {
$customer_email = test_input($_POST["customer_email"]);
if (!filter_var($customer_email, FILTER_VALIDATE_EMAIL)) {
$customer_email_error = "Invalid email format";
}
}
if (empty($_POST["customer_phone"])) {
$customer_phone_error = "Phone is required";
} else {
$customer_phone = test_input($_POST["customer_phone"]);
if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$customer_phone)) {
$customer_phone_error = "Invalid phone number";
}
}
if (empty($_POST["customer_message"])) {
$customer_message = "";
} else {
$customer_message = test_input($_POST["customer_message"]);
}
if ($customer_name_error == '' and $customer_email_error == '' and $customer_phone_error == '' ){
$customer_message_body = '';
unset($_POST['submit']);
foreach ($_POST as $key => $value){
$customer_message_body .= "$key: $value\n";
}
$to = 'gorilyawarfare@gmail.com';
$subject = 'Messages';
if (mail($to, $subject, $message)){
$success = "Message sent, thank you for contacting us!";
$customer_name = $customer_email = $customer_phone = $customer_message = '';
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>