我编写了一个联系我们页面并验证它我想要的是当用户点击发送按钮时信息应首先验证然后发送电子邮件给定的硬编码电子邮件地址我面临一个问题不知道为什么我的编码是不工作,验证没问题,但是当我点击提交按钮时,它没有显示有关电子邮件的任何成功消息,也没有发送电子邮件。请帮忙,这是我的代码:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Your Name: * </label>
<span style="color:#F00;" class="error"><?php echo $nameErr;?></span>
<input type="text" name="name" placeholder="Noman" width="100px;">
<label>Email Id: * </label>
<span style="color:#F00;" class="error"><?php echo $emailErr;?></span>
<input type="text" name="email" placeholder="yourname@gmail.com">
<label>Contact Number (Optional): </label>
<span style="color:#F00;" class="error"><?php echo $contactErr;?></span>
<input type="text" name="contact" placeholder="0312-1234567">
<label> Website URL (Optional):</label>
<span style="color:#F00;" class="error"><?php echo $websiteErr;?></span>
<input type="text" name="website" placeholder="www.yoursite.com">
<label> Message: * </label>
<span style="color:#F00;" class="error"><?php echo $messageErr;?></span>
<textarea name="message" rows="10" cols="10" placeholder="Message"></textarea>
<input type="submit" name="submit" value="Submit" id="submit">
</form>
这是我的PHP代码
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$name= $email= $contact= $website= $message="";
$nameErr = $emailErr = $contactErr = $websiteErr = $messageErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["contact"])) {
$contact = "";
} else {
$contact = test_input($_POST["contact"]);
if(!is_numeric($contact)){
$contactErr="Please enter valid mobile number";
}
else if(strlen($contact) != 11) {
$contactErr="Please enter valid mobile number";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
if (!preg_match( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["message"])) {
$messageErr = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
}
$from = $_POST["email"]; // sender
$subject = $_POST["name"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 200);
// send mail
mail("abc@gmail.com",$subject,$message,"From: $email\n");
echo "Thank you for sending us feedback";
?>
答案 0 :(得分:0)
我认为这是 标题 问题,
这只是一个例子,改变价值......
$header = "From: info@".$_SERVER["SERVER_NAME"]."\n";
$header .= "Content-Type: text/html; charset=utf-8\n";
$recipient = "abc@gmail.com"
$subject = $_POST['subject'];
$body='<table width="90%" border="0">
<tr>
<td><b>Name:</b></td> <td>'.$name.'</td>
</tr>
<tr>
<td><b>Email:</b></td> <td>'.$email.'</td>
</tr>
<tr>
<td><b>Message:</b></td> <td>'.$message.'</td>
</tr>
<tr></table>';
$res=@mail($recipient,$subject,$body,$header)
;