I made a simple contact form for my website. At the moment, after sending the message, it shows "The message is sent successfully" but doesn't really send. It can't deliver to the stated email address.
Why so? Is there anything wrong in my code? Also
jQuery(document).ready(function($){
the above function isn't working I guess.
My PHP file:
<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
";
$headers.="From:$name<$email>
";
$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.
-->
If you're trying to send the mail directly FROM the email entered by the user, some mail receivers will block mail from certain addresses and it will never get through, despite your SCRIPT thinking it sent successfully.
This was a problem I personally had with Wordpress's 'Contact Form 7', and found some other references to on-line.
It was solved by NOT using the email entered in the form, but using the site's contact email - 'contact@yoursite.com', and just referencing the email address in the body of the email.
As for jQuery's document ready -
$(function(){ //your code });