Php email form (MS server)
May 21, 2008
This post will show you how to link a html form with a php script for sending email through a web site. This php script only works on a Microsoft server.
I am using a unix server platform show me the linux version instead
The HTML FORM:
We want to allow the user to input 4 things: Name, Email Address, Subject and a Message. (N.B. We will actually need 5 input elements because a ’submit’ button is required to send the form data). The form should look something like this (HTML code follows):

<form method="post" action="sendform.php"> Name: <input type="text" name="userName" /> < /br>Email Address:<br /> <input type="text" name="userEmail" /> < /br>Subject:<br /> <input type="text" name="userSubject" /> </br>type your message:</br> <textarea name="userMessage" cols="20" rows="4" /></textarea> < /br> <input type="submit" onclick="validatethis()"/> </form>
Notice that we have given each input element a ‘name’ property. This allows the php script to identify the relevant data that it needs to send from the server. The ‘names’ in the html will also appear in the php script as named variables (information containers).
THE PHP SCRIPT:
Open a new notepad document and save it as ’sendform.php’. The name of the php script is named in line 2 of the html code above. The filename is case sensitive!
Let’s look at the part of the php script that makes the email happen:
send email
$name = $_REQUEST['userName'] ;
$email = $_REQUEST['userEmail'] ;
$subject = $_REQUEST['userSubject'] ;
$message = $_REQUEST['userMessage'] ;
$body = $name . $message;
ini_set("sendmail_from", " youremailaddress@yourserveraddress.com");
mail("youremailaddress@yourserveraddress.com", $subject, $body, "From: $email" );
header( "Location:http://www.yourdomain.com/YourThankYouPageAddress.htm" );
DON’T PANIC – It’ not that complicated:
- Line 3-6: The php is requesting the information the user has inputted. Notice that ‘userName, userEmail, userSubject and userMessage are the ‘names’ given to each input element in the html. Notice, also that the php ascribes this information to the variables $name, $email $subject and $message.
- Line 7: Tells the server what to put in the emails body. When the email is opened the main area will contain the senders’ name and message.
- Line 8: Makes php work on a Microsoft Server.
- Line 9: Tells the server how to structure the email. When the email arrives in your server’s inbox it will display the subject in the ’subject field’ and the email address in the ‘from field’. When you open the email it will display the body (line 7).
- Line 10: Redirects the user to a thankyou page after the form has been sent.
Remember, you have to change all the domains and file paths in the example script to match where you store your own files on your server.
The Entire php Script:
?php
function spamcheck($field)
{
//eregi() performs a case insensitive regular expression match
if(eregi("to:",$field) || eregi("cc:",$field))
{
return TRUE;
}
else
{
return FALSE;
}
}//if "email" is filled out, send email
if (isset($_REQUEST['email']))
{
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==TRUE)
{
header( "Location:http://www.yourdomain.co.uk/APageAskingTheUserNotToSpam.htm" );
}
else
{
//send email
$name = $_REQUEST['userName'] ;
$email = $_REQUEST['userEmail'] ;
$subject = $_REQUEST['userSubject'] ;
$message = $_REQUEST['userMessage'] ;
$body = $name . $message;
ini_set("sendmail_from", " youremailaddress@yourserveraddress.com");
mail("youremailaddress@yourserveraddress.com", $subject, $body, "From: $email" );
header( "Location:http://www.yourdomain.com/YourThankYouPageAddress.htm" );
}
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
Entry Filed under: How to: PHP EMAIL FORMS. .
Trackback this post | Subscribe to the comments via RSS Feed