using an existing token as a variable in a custom token I am using simplenews to send out a newsletter.  I have a custom module.  It has a table that relates a user code to the user's email.  

email_address                  user_code
bob.loblaw@lawblogs.com        37XylT12


In the simplenews email I will send, the idea is that each user will receive a link with their usercode in it (i.e., http://www.domain.com/?q=go&amp;c=37XylT12). 

I've been attempting to create a custom token to place in the text of the newsletter and replaced with my custom link prior to send.  However, I have two issues: 1) I cannot find an example of a token that includes a db_select query to return data and 2) I cannot find a way to access the simplenews email token ([simplenews-subscriber:mail]) in my custom module -- I need this value to lookup the code.  

If someone could point me in the right direction, I would appreciate it.
KMT
Accessing the database to set a token is not different from accessing the database in other cases.

function mymodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $sanitize = !empty($options['sanitize']);

  $replacements = array();

  if ($type == 'YOUR TOKEN TYPE') {
    foreach ($tokens as $name =&gt; $original) {
      switch ($name) {
        case 'YOUR TOKEN':
          $replacements[$original] = /* CODE TO QUERY THE DATABASE */;
          break;
      }
    }
  }

  return $replacements;
}


As for accessing the value of a token defined from a module, when you know the token you are looking for, you can call token_replace() passing the token for which you want to know the value as first parameter. Keep in mind that your implementation of hook_tokens() is going to be called again; be careful of not causing a loop.