I want to send a unique code to the user after registration. I know how to do custom modules. Also in the mail text send to the user I can see there are variables such as [user:name]. Should I define a variable and send it to the user?

Any help will be appreciated.


2 Answers 2


I assume you will have to store that unique code somewhere because the user is going to use it and you will have to verify it against something?

I'd suggest to define a field for that unique code, you can use the field permissions module or a custom hook_field_access() to hide from the user. Then you generate that unique code in hook_user_presave() for new users and save in that field. The advantage of this is that you can then enable the token.module and you get tokens for all fields for free.

Alternatively, you can also store it in a custom table or $user->data and define hook_token_info() and hook_tokens() yourself. Instead of directly defining it as a token, you can also define it as a property (hook_entity_property_info()) to entity.module and it will automatically be available as a token as well if the entity_token module is enabled.

So yes, whichever way you chose, in the end it will be a token and you can use it in the mail.

  • thanks for your answer. I can create the code and store it in a table. But the part I don't understand is about enabling the token.module to access tokens for all fields. How do you do that? Can you give me an example. Thanks Commented Jul 13, 2012 at 13:06
  • Well, if you store it in your own table, you need to implement hook_token_info() and hook_tokens() to declare your code as a custom token. See api.drupal.org/hook_token_info
    – Berdir
    Commented Jul 13, 2012 at 13:44
  • As I am new to Drupal it took me a while but I learnt a lot. Now, just one more thing please. I want to send the email with the generated code through the Account activation message (I can see my custom token at the bottom). The table where I store the code has the user UID. In the hook_tokens() implementation how can I get the user UID? In this way I can search for the code and replace it in the token. Thanks Commented Jul 14, 2012 at 23:52
  • You should receive the user object in $data. See api.drupal.org/api/drupal/modules%21user%21user.tokens.inc/… for an example.
    – Berdir
    Commented Jul 15, 2012 at 16:04

Should I define a variable and send it to the user?

If you allow the users with the right permission to set the content of the email your module sends to the user, and which contains the unique code, then you can define a token for that purpose. If that code needs to be used only for that email, I would implement code similar to the following one, which what Drupal uses when sending an email to the users to recover their password.

function mymodule_mail($key, &$message, $params) {
  $language = $message['language'];
  $variables = array('user' => $params['account']);
  $message['subject'] .= _mymodule_mail_text($key . '_subject', $language, $variables);
  $message['body'][] = _mymodule_mail_text($key . '_body', $language, $variables);

function _mymodule_mail_text($key, $language = NULL, $variables = array(), $replace = TRUE) {
  $langcode = isset($language) ? $language->language : NULL;

  switch ($key) {
    case 'user_unique_code_subject':
      $text = t('Your subject', array(), array('langcode' => $langcode));

    case 'user_unique_code_body':
      $text = t("Your body", array(), array('langcode' => $langcode));

  if ($replace) {
    return token_replace($text, $variables, array('language' => $language, 'callback' => 'mymodule_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));

  return $text;

function mymodule_mail_tokens(&$replacements, $data, $options) {
  if (isset($data['user'])) {
    $replacements['[user:unique-code]'] = mymodule_unique_code($data['user']);

When your module wants to send the email to the user, it uses drupal_mail().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.