Quick contact block

The code below is a solution to the following problem- Our organization has an e-mail announcements list that we haven't yet transitioned to a Drupal-based solution. So, right now new subscribers are added manually. We wanted an easy way for visitors to our site to send a request to be added to this list.

The code below is an adaptation of the code from contact module that makes this possible. Most of the form values are hard-coded (e.g. the subject and message) and then the contact module's form validation and submission functions are used to actually send the e-mail. Note that in this version the e-mail address is the only field that the user inputs. If you want to have the user enter other fields (e.g. name, or have the message field there labeled as phone#), take a look at the original code from contact_mail_page() .

Obviously the block title, etc. needs to be set appropriately. Also, you could hard code a particular contact (cid) rather than using the method below which chooses the default contact (if one has been specified), or otherwise chooses the contact with the lowest weight (the one listed first).

<?php
// this snippet is for Drupal 4.7.  It requires the contact module to be enabled and configured.

function quickcontact_form() {
  global
$user;
 
//for authenticated users, pre-fill fields with their name and e-mail.
 
if ($user->uid) {
   
$edit['name'] = $user->name;
   
$edit['mail'] = $user->mail;
  }

 
$default_category = 0;
 
 
$result = db_query('SELECT cid, category, selected FROM {contact} ORDER BY weight, category');
  while (
$category = db_fetch_object($result)) {  
   
// Select either the default ("selected") category, or the one with the lightest weight.
   
if ($category->selected) {
     
$default_category = $category->cid;
    }
    elseif (!
$default_category) {
     
$default_category = $category->cid;
    }
  }

  if (
$default_category > 0) {
   
$form['#token'] = $user->name . $user->mail;
   
$form['name'] = array('#type' => 'value',
     
'#value' => $edit['name'] ? $edit['name'] : 'Guest',
    );
   
$form['mail'] = array('#type' => 'textfield',
     
'#size' => 20,
     
'#maxlength' => 80,
     
'#default_value' => $edit['mail'],
    );
   
$form['subject'] = array('#type' => 'value',
     
'#value' => 'Subscription request',
    );
   
$form['cid'] = array('#type' => 'value',
     
'#value' => $default_category,
    );
   
$form['message'] = array('#type' => 'value',
     
'#value' => 'Subscription request for e-mail newsletter',
    );
   
$form['copy'] = array('#type' => 'value',
     
'#value' => FALSE,
    );
   
$form['submit'] = array('#type' => 'submit',
     
'#value' => 'Submit e-mail address',
    );
   
$output = drupal_get_form('quickcontact_form', $form,'contact_mail_page');
  }
  else {
   
$output = t('The contact form has not been configured.');
  }
  return
$output;
}

print
quickcontact_form();
?>

 
 

Drupal is a registered trademark of Dries Buytaert.