MENU | PHP: HTML Form 2 Email

PHP: HTML Form 2 Email

Assuming you have a very basic form already set up, using a submit button will allow you to call a php script:

<form method="post" action="contact.php">
<input name="email" type="text">
<textarea name="message" rows="15" cols="40"></textarea>
<input type="submit">
</form>

You’ll obviously format your form as you see fit, adding as many fields as you need to achieve what your after. The important bit is the PHP code you’ll use in “contact.php” to process the input of the form.

<?php
$to = "you@yoursite.com";
$subject = "Contact Us";
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

$_REQUEST takes the variables from the form - the “name” of each form element will be the name of the variable passed. For example “email” and “message” are the variables taken from the script and are the names of the form elements.

All that happens after this is the script takes all the variables and passes them to the php mail function which generates and sends an email to the address at the top of the script.

Easy

Filed by Kieran at July 30th, 2008 under Blog, Code, Geek!

Leave a comment

Weekly Top Artists

  • Loading...