Contact Us Form Using PHP Mail

You will need to have an HTML file first that will contain your form. Below is an example that you can edit to suit your needs. You can name it form.html, for instance.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Contact Us</title>
<link rel="stylesheet" type="text/css" href="../../css/nms.css" />
</head>
<body>
<form method="post" action="mail.php">
  <table>
   <tr>
    <td>What is your name ?</td>
    <td><input type="text" name="name" /></td>
   </tr>
   <tr>
    <td>What is your e-mail address ?</td>
    <td><input type="text" name="email" /></td>
   </tr>
    <tr>
    <td> Comments:</td>
    <td><textarea name="data" cols=40 rows=6></textarea></td>
    </tr>
   <tr>
    <td colspan="2"><input type="submit" /></td>
   </tr>
  </table>
</form>
</body>
</html>

A file that will process your form will need to be created. As you can see the form ‘action’ attribute on the code above, it calls the mail.php file. It will fetch the values from the form and sends it to your email address. See the code below.

<?
$to = 'me@mydomain.com';
$subject = 'subject';
$message = 'From: ' . $_REQUEST['name'] . “\n\n” . $_REQUEST['data'];
$email = $_REQUEST['email'];
$headers = ‘From: ‘ . $email . “\r\n” .
            ‘Reply-To: ‘ . $email . “\r\n” .
          ‘X-Mailer: PHP/’ . phpversion();

mail ($to, $subject, $message, $headers);
header(”Location: thanks.html”);
?>


Leave a Reply