Drupal 5: Creating a system for capturing email subscriptions using a CCK node type, a view, and a block module

A recent task that came my way way to create the functionality to capture email addresses using a simple subscription form. Since email is not a reliable means of capturing data, I used CCK to create nodes to capture the data and a view to show the results. First, I create a CCK node type called "email_subscription" which did not have a body or any custom fields, just the title for email address. I then created a view that shows the nodes in a table. Here's the module I created that generates a block module that contains the CCK node form. The block uses the CCK node form to capture the data. When the node is created, it uses the nodeapi hook to send a conformation email. There is also a function to validate the user's email address.

<?php
function MYMODULE_subscribe_block($op='list', $delta=0) {
  // listing of blocks, such as on the admin/block page
  if ($op == "list") {
    $block[0]["info"] = t('Subscribe Block');
    return $block;
  } else if ($op == 'view') {
    $block['subject'] = 'Subscribe';
    $block['content'] = _MYMODULE_subscribe_block_main();
    return $block;
  }
}

function _MYMODULE_subscribe_block_main() {
  // show the cck node form
  return drupal_get_form('email_subscription_node_form',
    array(
      'type' => 'email_subscription',
      'uid' => $GLOBALS['user']->uid,
      'name' => $GLOBALS['user']->name,
    )
  );
}

function MYMODULE_subscribe_form_alter($form_id, &$form) {
  if ($form_id == 'email_subscription_node_form') {
    // remove preview button
    unset($form['preview']);

    // add custom validation
    $form['#validate'] = array_merge(array('_MYMODULE_subscribe_node_form_validate' => array()), $form['#validate']);
  }
}

function _MYMODULE_subscribe_node_form_validate($form_id, $form_values, $form) {
  // validate title as email address
  if (!valid_email_address($form_values['title'])) form_set_error('title', 'Email is not valid.');

  // get list of already submitted emails
  $sql = "select title from {node} where type = 'email_subscription'";

  // if editing an existing node:
  if ($form_values['nid']) $sql .= " and title!='" . db_escape_string($form_values['title']) . "'";

  $resource = db_query($sql);
  $results = array();
  while ($row = db_fetch_array($resource)) $results[] = $row['title'];

  // ensure email address has not already been subscribed
  if (in_array($form_values['title'],$results)) form_set_error('title', 'Email has already been subscribed.');

}

function MYMODULE_subscribe_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type == 'email_subscription' && $op=='insert') {
    // send email to user
    mail($node->title, variable_get('site_name') . ' Subscription', 'Thank you for subscribing!', "From: " . variable_get('site_mail'));
  }
}
?>