Hello, I'm new to php and I'm trying to code my first contact form. The only problem I have is that I receive no email when I test it on my server. I have an index.php file and a handle_form.php file.

Here is my code... Am I doing something wrong? I deleted my email for spam purposes to post my code...

I have also used a php mail tester and it did work.....

Thanks

index.php

<div id="contactForm" class="container">
      
   <form action="handle_form.php" method="post">
      <fieldset><legend>Enter your information below:</legend>
      <table>
         <tr>
            <td><p>Name: </td> <td><input type="text" name="name" size="30" max length:"40" /> </p> </td></tr>
         <tr>
            <td><p>Phone: </td> <td> <input type="text" name="phone" size="30" max length:"40"> </p> </td></tr>
         <tr>
            <td><p>Email: </td> <td> <input type="text" name"email" size="30" max length:"60" /> </p> </td></tr>
         <tr>
            <td><p>Project Details: </td> <td> <textarea name="details" rows="8" cols="30"></textarea> </p> </td></tr>   
         <tr> <td> <input type="submit" name"submit" value="Submit" /> </td>   </tr>
      </table>
   </form>
   </div><!-- end of contactForm -->

handle_form.php

<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$details = $_POST['details'];

$to = 'example@gmail.com';
$subject = "New message: $name";
$message = "$name said: $details";
$headers = "From: $email";

mail($to, $subject, $message, $headers);

header("Location: ");
?>

perhaps you have not installed a mail server

Well I used this to test if the mail functionality was working. I did receive and email.

<?
$headers = 'From: webmaster@example.com';
mail('nobody@example.com', 'test email using PHP', 'This is a test email message', $headers, '-fwebmaster@example.com');
?>

<?php
$to = 'nobody@example.com';
$subject = 'test email using PHP';
$message = 'This is a test email message';
$headers = 'From: webmaster@example.com' . "\r\n" .
   'Reply-To: webmaster@example.com' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers, '-fwebmaster@example.com');
?>

Does anyone else maybe know a solution or a simple working contact form I could use?

Ok I did find some missing equal signs in my HTML Code. Am i missing anything else because I still can't get it to send to my email.

<div id="contactForm" class="container">    <form action="handle_form.php" method="post">      <fieldset><legend>Enter your information below:</legend>      <table>         <tr>            <td><p>Name: </td> <td><input type="text" name="name" size="30" max length:"40" /> </p> </td></tr>         <tr>            <td><p>Phone: </td> <td> <input type="text" name="phone" size="30" max length:"40"> </p> </td></tr>         <tr>            <td><p>Email: </td> <td> <input type="text" name="email" size="30" max length:"60" /> </p> </td></tr>         <tr>            <td><p>Project Details: </td> <td> <textarea name="details" rows="8" cols="30"></textarea> </p> </td></tr>            <tr> <td> <input type="submit" name="submit" value="Submit" /> </td>   </tr>      </table>   </form>   </div><!-- end of contactForm -->
<?php ob_start("ob_gzhandler"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>Email Contact form</title>
<?php include ("./menu.php"); // all the missing </head> and <body> are included in menu.php 
?> 
<P>Email</p>
<form action=<?php echo $_SERVER['PHP_SELF']; ?> method='post'>
<input type='hidden' value='Email from web form' name='subject'>
<input type='hidden' value="This is a string that is added to every message" name='message1'>
<input type='hidden' value='myname' name='to_name'>
<input type='hidden' value='myaddress' name='to_email'>
<div style='width:100%; float:left;'>
<div style='width:39%; text-align: right; float:left;'>From (Your name) :</div>
<div style='width:59%; text-align: left; float:right;'><input type='text' size='40' value='<?php if isset($_post['from_name']echo $_post['from_name']; ?>' name='from_name'>
</div></div>
<div style='width:100%; float:left;'>
<div class='required' style='width:39%; text-align: right; float:left;'>From (Your email) :
</div>
<div class='required' style='width:59%; text-align: left; float:right;'><input class='infor' type='text' size='40' value='<?php if isset($_post['from_email']echo $_post['from_email']; ?>' name='from_email'>
</div></div>
<div style='width:100%; float:left;'>
<div style='width:39%; text-align: right; float:left;'>Message :
</div>
<div style='width:59%; text-align: left; float:right;'><textarea name='message' cols='45' rows='6'><?php if isset($_post['message']echo $_post['message']; ?></textarea>
</div>
</div>
</div>
<center>
<button TYPE="submit" NAME="subm" VALUE="submit" onclick="return(confirm('Are all fields complete?'));" >Submit</button>
<button name="reset" TYPE="reset" VALUE="Reset" >Reset</button>
</center>
</form>
<?php If ($_post['to_email'] && $_post['message'] && $_post['subject']) {
$to = "\"$to_name\" <$to_email>";
$from = "\"$from_name\" <$from_email>";
$to = str_replace("\\'", "'", $to);
$from = str_replace("\\'", "'", $from);
$subject = str_replace("\\'", "'", $subject);
$message = str_replace("\\'", "'", $message);
mail($to, $subject, "Hi $to_name $message1 $message", "From: $from " . "\r\n" . "Cc: $from " . "\r\n" . "Bcc: mailcheck@yoursite.com"); // get copies of everything
echo "<div style='width:100%; float:left;'>Mail message sent  To : $to <BR> From : $from";
echo "\r\n Subject : $subject \r\n Message:'Hi' $to_name \r\n $message1 \r\n $message </div>";
} else {
echo '<div style="width:100%; float:left;"><font size="-1">This program does not check for correct address ';
echo 'formats, <BR>If the email addresses are not correct the receiving mail server ';
echo 'will not respond and mail will not be sent,<br> neither will it be saved.<br> ';
echo 'Please ensure the mail details are correct or your friend will not receive ';
echo 'the link.</font></div>';
} ?><p>&nbsp;</p></body></html><?php ob_flush(); ?>

Hmm that's tricky! Try something like this...

<!-- In the head -->
<script type="text/javascript" language="JavaScript">		
function sendmail() {
document.mailform.performaction.value="sendMail";
document.mailform.submit();
}
</script>

<!--Html around here-->

<div id="contactForm" class="container">
<form action="handle_form.php" method="post">
<fieldset><legend>Enter your information below:</legend>
<table>
  <tr>
    <td><p>Name: </td>
    <td><input type="text" name="name" size="30" max length:"40" /></p></td>
  </tr>
  <tr>
    <td><p>Phone: </td>
    <td><input type="text" name="phone" size="30" max length:"40"></p></td>
  </tr>
  <tr>
    <td><p>Email: </td>
    <td><input type="text" name="email" size="30" max length:"60" /></p></td>
  </tr>
  <tr>
    <td><p>Project Details: </td>
    <td><textarea name="details" rows="8" cols="30"></textarea></p></td>
  </tr>
  <tr>
    <td><input type="button" onclick="sendMail();" value="Submit" /></td>
  </tr>
</table>
<input type="hidden" name="performaction" value="" />
</form>
</div>
<!-- end of contactForm -->

Then in your PHP File.

if ($performaction == "sendMail") {
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$details = $_POST['details'];
 
$to = 'example@gmail.com';
$subject = "New message: $name";
$message = "$name said: $details";
$headers = "From: $email";
 
mail($to, $subject, $message, $headers);
}

Not promising anything... but if that doesn't work, I'd suggest looking at the SMTP Server.

Thanks for all the solutions. I will try this when I get home.

I also called godaddy and asked them a few questions. They said the main probelm was that email account like yahoo, gmail, hotmail, and etc. usually didn't make it throught the mail filter (basically spam).

Does anyone know if this is true for other hosts? Is this a standard thing?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.