我为我的网站制作了一个简单的联系表格。此时,在发送消息后,它显示“消息已成功发送”但未真正发送。它无法传送到指定的电子邮件地址。
为什么这样?我的代码有什么问题吗?还
jQuery(document).ready(function($){
以上功能我的工作无效。
我的PHP文件:
<div id="contact">
<!--
Contact form begins
-->
<div class="container">
<?php
$hasError=false;
$sent=false;
if(isset($_POST['submitform'])){
$name=trim(htmlspecialchars($_POST['name'],ENT_QUOTES));
$email=trim($_POST['email']);
$message=trim(htmlspecialchars($_POST['message'], ENT_QUOTES));
$fieldsArray=array(
'name'=>$name,
'email'=>$email,
'message'=>$message
);
$errorArray=array();
foreach($fieldsArray as $key=>$val){
switch($key){
case 'name':
case 'message':
if(empty($val)){
$hasError=true;
$errorArray[$_key]=ucfirst($key)."field was left empty.";
}
break;
case 'email':
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$hasError=true;
$errorArray[$key]="Invalid email address entered";
}else{
$email=filter_var($email, FILTER_SANITIZE_EMAIL);
}
break;
}
}
if(hasError!==true){
$to="jabirfatah91@yahoo.com";
$subject="Message from your website";
$msgcontents="Name: $name<br>Email:$email<br>Message: $message";
$headers.="MIME-version:1.0\r\n";
$headers.="From:$name<$email>\r\n";
$mailsent=mail($to, $subject, $messagecontents, $headers);
if($mailsent){
$sent=true;
unset($name);
unset($email);
unset($message);
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Conatct form</title>
<link rel="stylesheet" type="text/css" href="contactformdesign.css">
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
$("#contactform").validate({
rules:{
name:{
required:true,
minlength:2
},
email:{
required:true,
email:true
},
message:{
required:true,
minlength:50
}
},
message:{
name:{
required:"Please type your name",
minlength:"Your name seems a bit short"
},
email:{
required:"Please enter your email address",
email:"Please enter a valid email address"
},
message:{
required:"Please type your message",
minlength:"Your message seems a bit short. Please enter minimum 50 character"
}
}
});
});
</script>
</head>
<body>
<div class="container">
<h1>Contact form</h2>
<form id="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" novalidate>
<?php
if($sent===true){
echo "<h2 class='success'>Thanks, your message has been sent successfully</h2>";
}elseif($hasError===true){
echo '<ul class="errorlist">';
foreach($errorArray as $key =>$val){
echo "<li>".ucfirst($key)."field error-$val</li>";
}
echo '</ul>';
}
?>
<input type="text" name="name" value="<?php echo (isset($name)? $name: ""); ?>" placeholder="Your name">
<input type="email" name="email" value="<?php echo (isset ($email)? $email:"");?>" placeholder="Your E-mail">
<textarea name="message" placeholder="Your Message"><?php echo (isset($message)? $message: ""); ?></textarea>
<input type="submit" name="submitform" value="Send">
</form>
</div>
</body>
</html>
</div>
<!--
Contact form ends.
-->
答案 0 :(得分:0)
如果您尝试直接从用户输入的电子邮件发送邮件,一些邮件接收者将阻止来自某些地址的邮件,尽管您的SCRIPT认为它已成功发送,但它仍然无法通过。
这是我个人使用Wordpress的“联系表格7”时遇到的问题,并发现了其他一些在线参考。
通过不使用表单中输入的电子邮件,但使用网站的联系电子邮件 - “contact@yoursite.com”解决了这个问题,并且只是引用了电子邮件正文中的电子邮件地址。
至于jQuery的文档就绪 -
$(function(){ //your code });