PHP Code

Email form contents with PHP

Do you want to include a contact form on your website, but you don't know how to get the form information emailed to you? Well rather than using 3rd party components, you can easily use this PHP snippet to parse and email form contentst to the site administrator.


<?php
$name = $_POST&#91;'name'&#93;;
$email = $_POST&#91;'email'&#93;;
$body = $_POST&#91;'body'&#93;;

// check for valid input
if ($name != "" AND $email != "" AND $body != "") {

  $sendto = "admin@yoursite.com"; //change to your email address obviously 
  $subject = "Inquiry from yoursite.com"; 
  $message = "This is a message from your site: 
                   From: $name 
                   Email: $email 
                   Message: $body"; 
  // send 
  mail("$sendto", "$subject", "$message");
  echo "Your message was sent";
}
?>

and here's the simple html to generate the form


<form method="post" name="mailform" action="<?=$_SERVER&#91;'PHP_SELF'&#93; ?>">
 
<table width="200"  border="0" cellspacing="0">
  <tr>
    <td width="62">Name:</td>
    <td width="134">
      <input name="name" type="text" id="name">
   </td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input name="email" type="text" id="email"></td>
  </tr>
  <tr>
    <td valign="top">Message:</td>
    <td><textarea name="body" cols="45" rows="10"></textarea></td>
  </tr>
</table> 
  <br>
  <input type="submit" name="Submit" value="Submit">
  <input type="reset" name="Submit2" value="Reset">
</form>
Share

7 Comments

  1. Thank you for the code example and very easy to use instructions. I am a newbie to coding and was looking for a solution to hopefully mitigate some of the spam on the internet, while still allowing my client's customers a way to communicate via their website contact sheet.

  2. i was wondering where u put ur email in the html scruipt one i cannot figure this out

  3. $sendto = "admin@yoursite.com";

  4. thats in the php one not the html one 😛

  5. On the HTML page it's in this field:

  6. Thank you for the code example and very easy to use instructions. I am a newbie to coding and was looking for a solution to hopefully

  7. Hey there… a bit of trouble, I have the HTML section sorted so it's appearing on my webpage but when I test it out and click submit I get this error:

    Not Found

    The requested URL /< was not found on this server.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

    Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8l DAV/2 mod_auth_passthrough/2.1 FrontPage/5.0.2.2635 Server at touchdownrestaurant.com Port 80

    Any help?

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image