PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Quick and Clean PHP Forms
Pages: 1, 2, 3

Deploying the Contact Form

Up to this point, the actual email helper function used by the contact form has remained a mystery. Here is a possible implementation, saved in the aforementioned emailHelper.php file:

function emailValues (&$values) {
    $body = '';
    foreach ($values as $key=>$value) {
      if ($key != 'submit') {
        // replaces underscores with spaces 
        // and capitalizes the resulting words
        $body .= ucwords(ereg_replace('[_]', ' ', $key));
        $body .= "\r\n";
        if ($value == '') {
          $value = '(nothing entered)';
        $body .= $value;
        $body .= "\r\n\r\n";
    $subject = $values['subject'];
    $confirmation = "Thank you, your message has been sent.";

    require_once 'Mail.php';
    $smtp = Mail::factory('smtp');
    $headers = array ('From' => $from,
                      'To' => $to,
                      'Subject' => $subject);
    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
      $confirmation = $mail->getMessage() ;

    echo "<p>" . $confirmation . "</p>";

This function also takes advantage of the form field naming convention defined earlier to help format the email in a human-friendly way. Obviously, at the very least, you should consider extracting to a configuration file some of the strings hard-coded in this function.

Another PEAR library, Mail, which in turn requires the PEAR Net_SMTP library, is used instead of the built-in PHP mail function, as this approach is more flexible and easier to use in some circumstances. In particular, many people encounter issues with sendmail, the underling program called by the PHP mail function, and as most servers already have SMTP installed and available, using the SMTP method may be easier. SMTP may or may not be installed on your development machine, however, so consult your distribution's documentation if you encounter problems. On Ubuntu, for example, you can install SMTP using the following command:

sudo apt-get install postfix

HTML_QuickForm itself requires PHP4, the HTML_Common PEAR library and the PEAR base system. If you go to the HTML_QuickForm home page on the PEAR site, you will see that it has been superseded by HTML_QuickForm2, and its developers urge the use of that version. But if like many people, you are deploying onto a PHP4 installation, note that HTML_QuickForm2 requires PHP5, and as the original HTML_QuickForm is still being maintained with bug fixes and security updates, this is likely to be the version you will want to use. HTML_QuickForm2 was originally just a rewrite of HTML_QuickForm to make it work with the PHP5 E_STRICT setting, so the HTML_QuickForm documentation and examples should also be applicable to HTML_QuickForm2 with minimal changes. New functionality is also being added to HTML_QuickForm2, but these features have not been openly documented yet at the time this tutorial was written, so expect to read code to take advantage of any new features.

Finally, consider that often you may not have detailed knowledge of the environment onto which your code will ultimately be deployed or how that environment will be configured, particularly if you are a freelancer. For example, you may know that a client is running Apache2 and PHP4, but you may not know beforehand if they have the appropriate PEAR libraries installed and on the include path. And if the code doesn't work, you look bad, no matter the reason. So consider bundling the required PEAR libraries alongside the scripts that use them, so you can guarantee that all the dependencies will be met. For convenience, this is how the code for this tutorial has been packaged.


At this point, further attempts to optimize the email contact form may result in diminishing returns. As the new subclasses created above are used more, limitations in their implementation will likely become apparent, such as when more complex element types are needed, and further improvements can be made to address these issues as needed.

What has been presented so far is what the author has found helpful in his particular use of this library and for his style of development. As the reader uses HTML_QuickForm, she may choose to integrate the library into her tool set in a different manner that better fits her needs and her development style. The author has merely tried to demonstrate one way in which, with a small investment of time and thought, robust libraries like HTML_QuickForm can be leveraged further in certain contexts.

Of course, there is much more to the HTML_QuickForm library than what has been shown in this tutorial. Complete documentation can be found at the HTML_QuickForm site, including this list of features:

  • More than 20 ready-to-use form elements.
  • XHTML compliant generated code.
  • Numerous mixable and extendable validation rules.
  • Automatic server-side validation and filtering.
  • On request JavaScript code generation for client-side validation.
  • File uploads support.
  • Total customization of form rendering.
  • Support for external template engines (ITX, Sigma, Flexy, Smarty).
  • Pluggable elements, rules, and renderer extensions.

The important thing to remember is that because the optimizations introduced in this tutorial were implemented by wrapping existing functionality in subclasses of HTML_QuickForm, none of the existing functionality in the original super class has been limited in any way. The goal has been to make the most commonly used functionality of HTML_QuickForm a little more convenient to use for developers with certain needs. But if you need to do more than these new methods allow, you can still use any of the documented HTML_QuickForm functionality, including the addElement and addRule methods, as needed, right alongside the new subclass methods introduced in this tutorial.

Additionally, there are links on the HTML_QuickForm site to other PEAR packages that integrate with and extend the functionality of HTML_QuickForm in various ways, including the creation of CAPTCHA elements, database integration, and the creation of multi-page forms. In other words, its unlikely that using HTML_QuickForm, or the subclasses presented here, will limit you in any way as your forms evolve into the future.


Adam Smith is a senior software architect and project leader at Cornell University in Ithaca New York, where he has been focused most recently on developing Cornell's digital repository and digital preservation infrastructure. He is also the founder of Agile Enterprise Software providing businesses with complete print-to-digital marketing solutions. Read Adam's blog at

Return to ONLamp.

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: