Howto prevent php mail going spam

I am trying to create a PHP contact form. But the mail is going to spam. Can anyone please help me to fix this problem

pasting my code here

         <?php

	$error = "";
	
	$successMsg = "";

	if($_POST){
		
		
		
		if(!$_POST['email']){
			
			$error .= "An emailaddress is required<br>";
			
		}
		
		if(!$_POST['subject']){
			
			$error .= "The subject is required<br>";
			
		}
		
		if(!$_POST['content']){
			
			$error .= "The content field is required<br>";
			
		}
		
		if ($_POST['email'] && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == false) {
			
			$error .= "Not a valid email address";
		}
		
		if($error != ""){
			
			$error = '<div class="alert alert-danger" role="alert"><p><strong>There were error(s)in your form:</strong></p>' . $error .'</div>';
			
		}
		else{
			
			$emailTo = "asfdc@gmail.com" ;
			
			$subject = $_POST['subject'];
			
			$content = $_POST['content'];
			
			$headers  = "From: ".$_POST['email'] ;
			
			if(mail($emailTo,$subject,$content,$headers)){
				
				$successMsg = '<div class="alert alert-success" role="alert"><p><strong>Your msg was sent we\'ll get back to you ASAP!</strong></p></div>';
			}else{
				
				$error = '<div class="alert alert-danger" role="alert"><p><strong>your msg couldn\'t be sent-please try again later </strong></p></div>';
			}
			
		}
		
	}

	
?>

<!doctype html>
<html lang="en">
  <head>
  </head>
  <body>
 
	
	<div class="container">
    <h1>Get in touch!</h1>
	
	<div id="error">
	<? echo $error.$successMsg; ?>
	</div>
   
  
  
  
  <form method="post">
  
  
		
  
	  <fieldset class="form-group">
			<label for="email">Email address</label>
			<input type="email" class="form-control" id="email" name="email"
			placeholder="Email address">
	  </fieldset>
	  
  
	  <fieldset class="form-group">
			<label for="subject">Subject</label>
			<input type="text" class="form-control" id="subject" name="subject"> 
	  </fieldset>
	  
	  
	  <fieldset class="form-group">
			<label for="content">What would you like to ask us?</label>
			<textarea class="form-control" id="content" rows="3" name="content"></textarea>
	  </fieldset>

	 <button type="submit" id="submit" class="btn btn-primary">Submit</button>

</form>






  </div>
  
  
  
 

	<script type="text/javascript">
		
		$("form").submit(function (e) {
			
			
			
			var error="";
			
			if($("#email").val() == ""){
				
				error += "The email field is required<br>";
			} 

			if($("#subject").val() == ""){
				
				error += "The subject field is required<br>";
			} 
			
			if($("#content").val() == ""){
				
				error += "<p>The content field is required.<br>";
			}
			
			if(error != ""){
				
				$("#error").html('<div class="alert alert-danger" role="alert"><p><strong>There were error(s)in your form:</strong></p>' + error +'</div>');
			} 
			
				return false;
				
			else{
				
					return true;
			}
			
		});
	
	</script>
  
  
  </body>
</html>

The first thing I would try is changing this.
The user could be using an address from any domain.
Mail that has an address at one domain, but is actually being send by a different one is something that can trigger a spam alert, some servers don’t even allow it to happen…
Use a from address associated with your domain. You can then include the user’s email somewhere in the body if you need it.

2 Likes

Thank you for your response @SamA74.I tried this code. But it’s not fixed my problem.Mail is going to spam

That code is taken from your original post. It should be changed to an address from your domain.

$headers = 'From: something@yourdomain.com' ;

But you will probably also want to alter the $content to iclude the user’s email address.

1 Like

I tried this sir. It’s a contact form.so need the from details.

Yes, but they don’t have to be in the “from” email header. You could try using a “Reply-to” header if you really can’t just copy/paste the users from-address from the body of the email.

Most properly-configured email servers will not allow you to send emails from a domain that they are not responsible for. If I connect to the Gmail SMTP server (for example) and try to send an email using my own domain as the from-address, it will be rejected. To not reject it means that the server allows a thing called “Open Relay”, which is pretty much a no-no for a real mail server. If you run your own SMTP server, you could configure it to allow this, but it’s still not a good idea - someone will find it, use it, and you’ll get blacklisted.

2 Likes

Thank you @droopsnoot. I will try in that way.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.