--- htmlmail.module.orig	Wed Jul 15 07:12:42 2009
+++ htmlmail.module	Fri Aug 21 13:56:13 2009
@@ -66,23 +66,23 @@
   $message['body'] = is_array($message['body']) ? implode("\n\n", $message['body']) : $message['body'];
 
   // The paragraph an break stuff
-  if (variable_get('htmlmail_autop', '1') == 1) {
+  if (variable_get('htmlmail_autop', '1')) {
     $message['body'] = _filter_autop($message['body']);
   }
 
   // Note: this is now coming from theme template html.tpl.php
   // Insert the preformatted HTML so the end user only needs to enter what goes between the <body> tags.
   // this of course can be overridden in the admin settings for this module.
-  if (variable_get('htmlmail_preformat', '1') == 1) {
+  if (variable_get('htmlmail_preformat', '1')) {
     $message['body'] = theme('htmlmail', $message['body']);
   }
 
-  if (variable_get('htmlmail_urlfilter', '1') == 1) {
+  if (variable_get('htmlmail_urlfilter', '1')) {
 		// defaults to 72 as there is no filter 0 -- make filters a configuration option?
     $message['body'] = _filter_url($message['body'], 0);
   }
 
-  if (variable_get('htmlmail_emogrifier', '0') == 1) {
+  if (variable_get('htmlmail_emogrifier', '0')) {
     $message['body'] = _htmlmail_emogrify($message);
   }
 }
@@ -91,12 +91,43 @@
  * Implementation of hook_theme().
  */
 function htmlmail_theme() {
-  return array(
-    'htmlmail' => array(
-      'template' => 'htmlmail',
-      'arguments' => array('body' => NULL),
-    ),
-  );
+  $items['htmlmail'] = array(
+    'template' => 'htmlmail',
+    'arguments' => array('body' => NULL),
+    );
+  
+  if (module_exists('token') && variable_get('htmlmail_token', '0')) {
+    $items['htmlmail_token_help'] = array(
+      'arguments' => array('prefix' => '[', 'suffix' => ']'),
+      );
+  }
+  
+  return $items;
+}
+
+/**
+ *  Token support
+ */ 
+
+function theme_htmlmail_token_help($prefix = '[', $suffix = ']') {
+  token_include();
+
+  $full_list = array_merge(token_get_list('user'), token_get_list('profile'));
+
+  $headers = array(t('Token'), t('Replacement value'));
+  $rows = array();
+  foreach ($full_list as $key => $category) {
+    $rows[] = array(array('data' => drupal_ucfirst($key) .' '. t('tokens'), 'class' => 'region', 'colspan' => 2));
+    foreach ($category as $token => $description) {
+      $row = array();
+      $row[] = $prefix . $token . $suffix;
+      $row[] = $description;
+      $rows[] = $row;
+    }
+  }
+
+  $output = theme('table', $headers, $rows, array('class' => 'description'));
+  return $output;
 }
 
 /**
@@ -105,8 +136,16 @@
  * @see htmlmail.tpl.php
  */
 function template_preprocess_htmlmail(&$variables) {
-  $variables['header'] = variable_get('htmlmail_header', '');
-  $variables['footer'] = variable_get('htmlmail_footer', '');
+  if (module_exists('token') && variable_get('htmlmail_token', '0')) {
+    global $user;
+    $types = array('user' => $user, 'profile' => $user);
+  
+    $variables['header'] = token_replace_multiple(variable_get('htmlmail_header', ''), $types);
+    $variables['footer'] = token_replace_multiple(variable_get('htmlmail_footer', ''), $types);
+  } else {
+    $variables['header'] = variable_get('htmlmail_header', '');
+    $variables['footer'] = variable_get('htmlmail_footer', '');
+    }
   $variables['css'] = variable_get('htmlmail_css', '');
 }
 
