Index: token_profile.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/token_profile/token_profile.module,v
retrieving revision 1.2.2.1
diff -u -p -r1.2.2.1 token_profile.module
--- token_profile.module	29 Jul 2010 14:46:32 -0000	1.2.2.1
+++ token_profile.module	8 Feb 2011 08:53:28 -0000
@@ -117,3 +117,63 @@ function _token_profile_format_field($fi
       return check_plain($field->value);
   }
 }
+
+/**
+ * Implementation of hook_mail_alter().
+ *
+ * @param Array $message A structured array containing the message to be altered. keys in this array include: id, to, subject, body, from, headers
+ */
+function token_profile_mail_alter(&$message) {
+  //check to see that it is the user module that is sending the email
+  switch ($message['id']) {
+    case 'user_register_admin_created':
+    case 'user_register_no_approval_required':
+    case 'user_register_pending_approval':
+    case 'user_password_reset':
+    case 'user_status_activated':
+    case 'user_status_blocked':
+    case 'user_status_deleted':
+      //load the profile tokens array to replace in the message
+      $profile_tokens = _token_profile_get_token_values($message['to']);
+      
+      //check to see that something was loaded
+      if ($profile_tokens === FALSE) {
+        return;
+      }
+      
+      //modify the message and subject to replace the profile tokens
+      $message['subject'] = strtr($message['subject'], $profile_tokens);
+      foreach ($message['body'] as $i => $body) {
+        $message['body'][$i] = strtr($message['body'][$i], $profile_tokens);
+      }
+      
+      break;
+  }
+}
+
+/**
+ * Builds a keyed array of profile tokens and values for a user that can be used in strtr
+ * 
+ * @param String $email The email address of the user to load
+ * 
+ * @return Array A keyed array with the keys the profile tokens and the values the users profile values
+ */
+function _token_profile_get_token_values($email) {
+  //attempt to load the user fomr the email
+  $user = user_load(array('mail' => $email));
+  if ($user === FALSE) {
+    return FALSE;
+  }
+  
+  //load the users profile
+  profile_load_profile($user);
+  $fields = _token_profile_profile_fields();
+  
+  $values = array();
+  foreach ($fields as $name => $field) {
+    $field->value = $user->$name;
+    $values['!' . $name] = _token_profile_format_field($field);
+  }
+
+  return $values;
+}
\ No newline at end of file
