Programatically creating a contact form in Drupal 7

By default, when the Drupal core Contact module is enabled and configured, Drupal automatically creates pages with contact forms for you. Unfortunately the pages themselves look quite bland, and cannot be directly edited. It is possible to use the Contact Form Blocks module to create a contact form in a block and embed that in another page - though this module does not have a stable release for Drupal 7 at the time of writting.

Another easy solution, one which gives more flexibility than using the Contact Form Blocks module, is to generate the contact form programatically. It's only two lines of code :


This can be included at the theme level, or in a node or block as long as you select an input format that supports PHP code.