Location: PHPKode > projects > Modular Site Manager > htdocs/tutorials/PHP-24.php
This tutorial is to show you how to use the mail function, to create a mega good contact form. I may upgrade this some time to have stuff like email address checking to prevent even more spam.<br />

<br />

First we create the frontend, which is what your visitors will see<br />

<div class="code"><strong>CODE:</strong><br />
<hr /><?php xhtml_highlight('<form name=\"contact\" method=\"post\" action=\"post.php\"><br />

  <p> Name <br />

    <input type=\"text\" name=\"name\"><br />

    <br><br />

    Email: <br />

    <input type=\"text\" name=\"email\"><br />

    <br><br />

    Subject of message: <br />

    <input type=\"text\" name=\"subject\"><br />

  <br><br><br />

    Message -<br><br />

    <textarea name=\"message\"></textarea><p></p><br />

	<input type=\"submit\" name=\"Submit\" value=\"Contact Us\" /><br />

</form><br />

<br />

'); ?></div><br />

<br />

Now, thats pretty self explanitory. You can save that as whatever you want. Its simply a form that lets your visitor input all the details they want.<br />

<br />

Now we need to create the backend, post.php. I have explained what little bit does in the comments (// comment)<br />

<br />

<div class="code"><strong>CODE:</strong><br />
<hr /><?php xhtml_highlight('<? // php 101, this is to say that your using php... sheesh duh...<br />

// First we determine whats what<br />

$name = $_POST[\'name\'];  // the $_post function gets whatever the user put in to the text box<br />

$email = $_POST[\'email\'];<br />

$subject = $_POST[\'subject\'];<br />

$message = $_POST[\'message\'];<br />

<br />

// now we make sure the little bastards didnt leave anything empty (except subject since that doesnt matter)<br />

if (empty($name) || empty($email) || empty($message)) {<br />

   echo \'Please go back and fill in the empty field mate, or i will rape your mother to timbucktoo\';<br />

   exit ;<br />

}<br />

else {<br />

// So, all the forms have something in them, now we are going to have to clean up the message first though, so you can make sense of it<br />

$message = wordwrap($message, 70); // adds wordwrap to the message<br />

$message = str_replace(\"/n\", \"\\n\", $message); // This should just make sure with the new lines, might not be neccesary though<br />

<br />

// Now, using the php mail function, we will send the email<br />

if(mail(\'hide@address.com\', $subject, $message, \"From: \\\"$name\\\" <$email>\\r\\nReply-To: \\\"$name\\\" <$email>\")) {<br />

echo \'Thankyou for your email, it has been successfully sent.\';<br />

} else { // This is to say, that if the mail function fails for some shit reason, it will tell the person<br />

echo \'Sorry, god doesnt like you, and says you may not send it.\';<br />

}} // ends the \"if empty\" statement, and \"if mail\" statement, just to tell the little php thing its done.<br />

?> '); ?></div><br />

<br />

Now, thats you done, you just need to save that code above as post.php, and edit the little bit that says \"hide@address.com\" to your email, and whoila, a contact form.
Return current item: Modular Site Manager