Added: 18 February, 2008
Group: PHP
PHP form processing and email sending
Author: Alex
page: 1

PHP form processing and email sending

One of most common usage of HTML form on websites are sending information e-mails. In this simple tutorial you will learn how to create HTML form and send email with little php code.


One of most common usage of HTML form on websites are sending information e-mails. In this simple tutorial you will learn how to create HTML form and send email with little php code.


First, in your favorite text editor create this simple form
<form action="page.php" method="post">
Name: <input type="text" name="name">
Comment: <input type="text" name="comment">
<input type="submit" name="Submit" value="Send">


As you can see it is basic form with just Name, Comment field and Submit button. That should be enough if you want to get feedback information or just a comment on your web page. As option there can be created e-mail filed, so you can reply to comment.

Next step is creating page.php file. Again in your text editor create this code:

<?php
mail("your@email.com","Email Subject","
Body of your message
$_POST[name]
$_POST[comment]
","From: site@yourserver.com");
?>


This PHP file will be called when user click Send button in HTML form and with PHP function mail() it will send email to the specified address.

Now lets, discuss some details. If you look closely the Name and Comment information in this PHP code are retrieved with $_POST variable. The $_POST variable is used to collect values from a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
It is wise to use this way of information processing if you want to keep privacy of details that users provide.

And that should be all you need to create simple form processing and email sending with PHP. Additionally this code can be extended with code for checking if information is entered into fields, checking validity of e-mail user has provided or formatting the text in Comment field.

GO to: Page 1 : PHP form processing and email sending


TechTut.com This tutorial is copyrighted. Partial duplication or full duplication is prohibited and illegal. Translation or usage of any kind without author�s permission is illegal.