• 0

Basic HTML5/CSS/PHP Contact Us Form not working


Question

Taking a web development course and one thing I'm stuck on is my form submit isn't actually sending an email to me. The scrip redirects to the home page, but I never get the email. I've been looking at the tutorials online, but nothing seems to get through (and I've checked Spam too). The text book I have only seems to cover the HTML/CSS Side of things and not the PHP part.

 

I've uploaded the files to a free host (awardspace.com) that says they have PHP enabled.

 

HTML and CSS

<form action="contactus.php" method="post" enctype="multipart/form-data">
	        
	        <h1 class="title">Contact</h1>
	        
		    <label></label>
		    <input name="name" required placeholder="Your Name">
		    
		            
		    <label></label>
		    <input name="email" type="email" required placeholder="Your Email">
		            
		    
		    <label></label>
		    <textarea name="message" cols="20" rows="5" required placeholder="Message"></textarea>
		    
		    		    
		    <input id="cancel" name="cancel" value="Cancel" />
		            
		    <input id="submit" name="submit" type="submit" value="Submit">
	        
	    </form>
<?php 

if(isset($_POST['email'])) {
	
	$email_to = "musicmaster@gmail.com";
	$email_subject = "Mail from Web form";
	$thankyou = "index.html";
	
	function died($error) {
		echo "Sorry, but there were error(s) found with the form you submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}
	
	
	$name = $_POST['name']; 
	$email = $_POST['email'];  
	$message = $_POST['message']; 
	
	
	$email_message = "Message details below.\r\n";
	
	function clean_string($string) {
	  $bad = array("content-type","bcc:","to:","cc:","href");
	  return str_replace($bad,"",$string);
	}
	
	$email_message .= "name: ".clean_string($full_name)."\r\n";
	$email_message .= "email: ".clean_string($email_from)."\r\n";
	$email_message .= "message: ".clean_string($comments)."\r\n";
	
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
header("Location: $thankyou");
?>
<script>location.replace('<?php echo $thankyou;?>')</script>
<?php
}
die();
?>
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Perhaps that host has disabled the mail() function. I know certain versions of wamp and stuff no longer bundle the ability to use mail() and third party libraries/tools are required.

 

My first step would be to make sure PHP is even being run. Then making sure the host supports mail().

 

 

Also, for reference:

http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail

Link to comment
Share on other sites

  • 0

In your PHP code to send email, you have written two functions inside if() block which is incorrect. Write those two functions before or after the if block ends.

Check the code below. I put died() and clean_string() functions before if() block starts.

<?php

function died($error) 
{
		echo "Sorry, but there were error(s) found with the form you submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
 }

function clean_string($string) {
	  $bad = array("content-type","bcc:","to:","cc:","href");
	  return str_replace($bad,"",$string);
	}
	
	
	
if(isset($_POST['email'])) 
{
	
	$email_to = "musicmaster@gmail.com";
	$email_subject = "Mail from Web form";
	$thankyou = "index.html";
	 
	$name = $_POST['name']; 
	$email = $_POST['email'];  
	$message = $_POST['message']; 
	 
	$email_message = "Message details below.\r\n";
	
	
	$email_message .= "name: ".clean_string($full_name)."\r\n";
	$email_message .= "email: ".clean_string($email_from)."\r\n";
	$email_message .= "message: ".clean_string($comments)."\r\n";
	
	$headers = 'From: '.$email_from."\r\n".
	'Reply-To: '.$email_from."\r\n" .
	'X-Mailer: PHP/' . phpversion();
 	@mail($email_to, $email_subject, $email_message, $headers);
 	header("Location: $thankyou"); 
	
}
die();

?>
Link to comment
Share on other sites

This topic is now closed to further replies.