Syndicate content
> PHP Mail Form Function : PHP Form-to-Mail Script

PHP Mail Form Function : PHP Form-to-Mail Script

PHP Form-to-Mail Script


PHP Form-to-Mail Script
There's usually alot of people who would like to try to implement some kind of form-to-mail script. Unfortunately, the most popular open-source mail script is written in Perl and is SO well known it is easily the most exploited. Many hosts actually disable Matt Wright's FormMail for this very reason.

 

Never fear. PHP to the rescue.

Step 1: Create your form

Here's a simple form I might use

<form action="process.php" method="post">
Name: <input type="text" name="name" size="20" maxlength="20" /><br />
Email: <input type="text" name="email" size="30" maxlength="30" /><br />
Subject: <input type="text" name="subject" size="30" maxlength="30" /><br />
Text:<textarea name="text" name="text" cols="50" rows="10"></textarea><br />
<input type="submit" name="submit" value="Send" />
</form>

Save this file as form.php. now for a little explanation.

You'll notice I used process.php as my form action. This is because the form data is going to be submitted to that file for actual mailing. I also wanted to limit how much data could be submitted. This is for security reasons. There is NEVER a reason why you shouldn't limit form data. Just so you know. ;)

Step 2: Processing the data

Now create a file called process.php.

First you want to get all the variables that were just sent to the page. Using the extract function, we can create identically named variables out of the $_POST array. In English this means that all the variables that have come to this page via the POST method (see the form above) will now be made into nice single-word variables.

@extract($_POST);

Now we want to make the data friendly for us.

$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$text = stripslashes($text);

Now we can do our mailing.

mail('youremail@domain.com',$subject,$text,"From: $name <$email>");

And finally redirect the user back to the form:

header("location:form.php");

So our code for process.php looks like this:

<?php
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$text = stripslashes($text);
mail('youremail@domain.com',$subject,$text,"From: $name <$email>");
header("location:form.php");
?>

There's plenty more you can do with mail().

Check out the mail function here.

Aaron
Aaron Brazell
www.emmense.comAaron owns and operates Emmense Technologies, a Web development company geared toward non-profit clients. He is also Partner and System Admin for .
Barefooting.com Web Hosting Services

User login

Christian-Web-Masters.com newsletter

Stay informed on our latest news!