26 September, 2005

Emailing Forms with PHP

0 Comments

Written by Jason Davies Topics: PHP

So, you want a nice little feedback form on your Web site. The user clicks the submit button, and the feedback makes its happy way to your email address. Let's look at how this is done in PHP.

Example PHP Code

Here's some example PHP code to start us off:

<html>
    <head><title>Feedback</title></head>
    <body>

    <?php

        // Handle POST method.
        if ($_POST)
        {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $comments = $_POST['comments'];

            // Compose simple text message:
            $message = "Message from $name ($email)

Comments:

$comments";

            // Send message to bob@microsoft.com
            mail("bob@microsoft.com", "Feedback", $message);

            // Thank the generous user
            echo "<h1>Cheers!</h1>";
        }
        else
        {
    ?>
        <h1>Feedback</h1>
        <form action="<?= $PHP_SELF ?>" method="post">
            <p>Name: <input type="text" name="name" /></p>
            <p>Email: <input type="text" name="email" /></p>
            <p>Comments:</p>
            <p><textarea name="comments"></textarea></p>
            <p><input type="submit" value="Send!" /></p>
        </form>
    <?php
        }
    ?>
    </body>
</html>

Notice how the program does two things: if it is handling a POST request then it sends the email and displays a thank you message. Otherwise it displays the feedback form.

The POST Request

When you click on that submit button, what happens?

The browser sends all the form fields to the server using a POST request. A POST simply means that all the submitted fields are sent in the body of the request. In a GET request, the submitted fields are appended to the URL e.g. feedback.php?name=Jason. The POST method is preferable here because the comments field might exceed the browser's URL length limit if a GET request was used.

So the fields are sent to the server in the body of the request. In fact, the field names and their values are sent. When the PHP script handles the request, it puts these values into an associative array called $_POST, with the field names as the keys. So the value of the name field could be accessed using $_POST['name'].

Sending the Email

First we create a $message variable containing the body of the message. Then we call the mail function with the appropriate parameters to send the actual email.

HTML Emails

To send a HTML email we need to add the appropriate Content-Type header. This is done by adding another parameter to the call to mail.

$headers  = "MIME-Version: 1.0";
$headers .= "Content-type: text/html; charset=iso-8859-1";

mail("bob@microsoft.com", "Feedback", $message, $headers);

Read the Manual!

For more information, please read the excellent PHP manual for the mail() function, which contains useful examples and notes.

0 Responses to “Emailing Forms with PHP”

Leave a Comment