Answers.Body: <p><strong>Option 1: Use the CiviToken extension [recommended, CMS independent]</strong>: 
  Fuzion's <a href="https://github.com/eileenmcnaughton/nz.co.fuzion.civitoken" rel="nofollow noreferrer">CiviToken</a> extension provides <code>{user.name}</code> and <code>{user.id}</code> tokens by default. (<em>Pro tip:</em> this module also makes it easy to add custom tokens in a <em>tokens/</em> folder in your CiviCRM custom PHP directory; you can duplicate and modify a file from the <em>tokens/</em> folder in this extension)</p>
  
  <p><strong>Option 2: Custom token in your own Drupal module [Drupal specific]</strong>: 
  <a href="https://civicrm.stackexchange.com/users/4/coleman">Coleman</a> authored a blog post some years back which outlined <a href="https://civicrm.org/blogs/colemanw/create-your-own-tokens-fun-and-profit" rel="nofollow noreferrer">how to make custom tokens</a>. The following snippet is based on that and when slipped into a custom module will make the Drupal username available as a token in CiviMail.</p>
  
  <pre><code>&lt;?php
  /* CiviCRM token addition for Drupal username */
  
  function MYMODULE_civicrm_tokens(&amp;$tokens) {
    $tokens['username'] = array(
      'username.drupal' =&gt; 'Drupal username',
    );
  }
  
  function MYMODULE_civicrm_tokenValues(&amp;$values, $cids, $job = null, $tokens = array(), $context = null) {
    // Username tokens
    if (!empty($tokens['username'])) {
  
      civicrm_initialize();
      foreach ($cids as $cid) {
        $params = array(
          'version' =&gt; 3,
          'sequential' =&gt; 1,
          'contact_id' =&gt; $cid,
        );
        $result = civicrm_api('UFMatch', 'get', $params);
        if (!$result['is_error']) {
          $drupalinfo = user_load($result['values'][0]['uf_id']);
          $values[$cid]['username.drupal'] = $drupalinfo-&gt;name;
        }
      }
  
    }
  }
  ?&gt;
  </code></pre>
  
  <p>Keep in mind that this is the bare bones and doesn't include any handling of a contact without a Drupal account. (So you would want to make sure to only use this token when sending to contacts with a Drupal account.)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T19:15:04.110Z
Answers.Text: nice example :)
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-02T01:05:15.593Z
Answers.Text: Even better than avoiding non-users, you could just have an "else" that returns something innocuously generic or suggests that the recipient create an account.
Answers.UserDisplayName: Andie Hunt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-02T23:11:24.407Z
Answers.Text: I think it's better not to produce a value if there isn't one. People can then use Smarty to provide a default value foe non-user contacts if desired.
Answers.UserDisplayName: Joe Murray
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T16:41:35.310Z
Answers.LastActivityDate: 2016-01-19T20:50:19.263Z
Answers.LastEditDate: 2017-04-13T12:53:13.207Z
Answers.UserDisplayName: Laryn
Body: <p>From time to time we need to send messages through CiviMail that include a contact's CMS (Drupal/Wordpress/Joomla) username or ID. How can we add this to the available tokens?</p>
  
Tags: <civimail><wordpress><drupal><joomla><tokens>
Title: How can I reference a contact's Drupal/Wordpress/Joomla account with a CiviMail token?
ThreadText: # How can I reference a contact's Drupal/Wordpress/Joomla account with a CiviMail token?
  
  (Asked by: username_0 on 2015-04-01)
  
  From time to time we need to send messages through CiviMail that include a contact's CMS (Drupal/Wordpress/Joomla) username or ID. How can we add this to the available tokens?
  
  **Tags:** civimail, wordpress, drupal, joomla, tokens
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-04-01. Score: 5 | Accepted answer
  
  **Option 1: Use the CiviToken extension [recommended, CMS independent]**:
  Fuzion's [CiviToken](https://github.com/eileenmcnaughton/nz.co.fuzion.civitoken) extension provides `{user.name}` and `{user.id}` tokens by default. (*Pro tip:* this module also makes it easy to add custom tokens in a *tokens/* folder in your CiviCRM custom PHP directory; you can duplicate and modify a file from the *tokens/* folder in this extension)
  
  **Option 2: Custom token in your own Drupal module [Drupal specific]**:
  [username_1](https://civicrm.stackexchange.com/users/4/username_1) authored a blog post some years back which outlined [how to make custom tokens](https://civicrm.org/blogs/colemanw/create-your-own-tokens-fun-and-profit). The following snippet is based on that and when slipped into a custom module will make the Drupal username available as a token in CiviMail.
  
  ```
  <?php
  /* CiviCRM token addition for Drupal username */
  
  function MYMODULE_civicrm_tokens(&$tokens) {
    $tokens['username'] = array(
      'username.drupal' => 'Drupal username',
    );
  }
  
  function MYMODULE_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
    // Username tokens
    if (!empty($tokens['username'])) {
  
      civicrm_initialize();
      foreach ($cids as $cid) {
        $params = array(
          'version' => 3,
          'sequential' => 1,
          'contact_id' => $cid,
        );
        $result = civicrm_api('UFMatch', 'get', $params);
        if (!$result['is_error']) {
          $drupalinfo = user_load($result['values'][0]['uf_id']);
          $values[$cid]['username.drupal'] = $drupalinfo->name;
        }
      }
  
    }
  }
  ?>
  
  ```
  
  Keep in mind that this is the bare bones and doesn't include any handling of a contact without a Drupal account. (So you would want to make sure to only use this token when sending to contacts with a Drupal account.)
  
  > **Comments:**
  > **username_1**: nice example :)  
  > **username_2**: Even better than avoiding non-users, you could just have an "else" that returns something innocuously generic or suggests that the recipient create an account.  
  > **username_3**: I think it's better not to produce a value if there isn't one. People can then use Smarty to provide a default value foe non-user contacts if desired.  
  
  ---
  
  