I tested this module and noticed that the token !username in the mail templates does not use the themed username. Would it be possible to make this use the themed username?

Thank you!

Members fund testing for the Drupal project. Drupal Association Learn more


salvis’s picture

We're producing plain text.

How would themed plain text differ from unthemed plain text?

YK85’s picture

Hi salvis!

Printing the themed username allows the preferred username to be used for sites where username has been customized (themed) in template.php or for sites that use the Realname module that allows a Profile field or CCK field to be shown as a user's username. With the themed username, an email would read "Hello John, ..." but currently the unthemed username is making it read "Hello j8km, ...". If the username is not customized or Realname module is not used, the default username would shows.

As an example, the User Relationships module prints the themed username for messages etc but had several occurrences of username that wasn't printing the themed username which was recently corrected in #719182: Username gets not themed in all cases a default message is shown and the commit can be viewed here.

I am not a programmer and don't know how difficult it is to implement, but I would really appreciate it if you would consider adding this functionality of printing the themed username.

Thank you!

salvis’s picture

Title: make !username use the themed username » Add themed versions of the user names

Thank you for the snippet, that is helpful. I boils down to

'!name' => theme('username', $user->name)

However, the problem with theme() is that it generally produces HTML, to be displayed in a browser, and we need text.

We're using a core function to reduce the HTML to text, but this leaves left-overs. For example, if the theme makes the username bold, then it would appear as "Hello *John*,"; very often the username is rendered as a link, which would come out as "Hello John [1]," with an entry for [1] at the bottom.

We might try to use check_plain() on the themed username, but I'm not sure whether the result would be satisfactory.

We definitely can't replace the existing variables with unexpected values, but we might add themed versions of the usernames. This is another issue: we have more than one username in the Subscriptions variables, and we already have lots of variables.

I'll need to see some support for this idea and ideally a patch...

Bilmar’s picture

+1 subscribing

I am currently using this awesome module but having to avoid using the !username because it isn't printing out what I needed =/ This would be a great new addition to this module and for my site would add a better feel for users as they will be addressed by name in the emails. I think doing a check plain to make the themed username work in the email would be great (removing any bold, italics etc)

I apologize that I am a web designer and do not know programming to be able to contribute a patch. I will be available to immediately test a patch and provide feedback.


salvis’s picture

Title: Add themed versions of the user names » Add themed versions of the user names; replace *_realname?

In what context are you using Mail Editor? With Subscriptions or not?

Mail Editor only has !username.

Subscriptions already provides !recipient_name, !recipient_realname, !sender_name, !sender_realname, !comment_name, !comment_realname. Would the *_realname strings be the same as the *_themed versions, so that the latter could replace the former?

Bilmar’s picture

Hi salvis,

I'm not using Subscriptions. I'm using LoginToboggan module and setting the English and Japanese message templates such as the one below and only see these replacement patterns.

(Welcome message when user self-registers)

Replacements patterns:

Adding the *_realname replacements such as !recipient_realname would be great! I am not sure if this solves the issue for users not using Realname module and theming the username via template.php though.


bryancasler’s picture

Exactly what I am looking for. Subscribe!

YK85’s picture

same as #6 for me
appreciate you looking into this!

salvis’s picture

Title: Add themed versions of the user names; replace *_realname? » Add !username_themed; replace Subscriptions' *_realname?
1.05 KB

According to #2, the Realname module provides the 'realname' through theming, so I assume that theme_username() will produce the same result.

If this is correct, I'll change Subscriptions to use theme_username(), too, because that is a more general mechanism.

Please try the attached patch.

bryancasler’s picture

The module and patch installed correctly, but it acted as if the module was not installed. This did not seem to work for me, the over ride never happened for me. The original emails just went out.

salvis’s picture

Status: Active » Needs review

You should see the new !username_themed variable in the list of available variables and you'll have to change your templates manually.

bryancasler’s picture

In my situation before I had clicked the "en" link and added that variable to the template, but it still didn't work.

Bilmar’s picture


I confirmed that the patch does not work =( The replacement pattern !username_themed does show as available and I added it into the email template for logintoboggan_register_no_approval_required (Welcome message when user self-registers). But checking the email that is sent shows that !username_themed prints the following in the email template:


The themed username should be showing 'billy' but it is showing as trupal218_themed, so it seems it is printing !username followed by _themed instead.

Additionally, I tried changing !username_themed to !themed in the patch and now it prints 'Guest' (name for Anonymous user) in the email. Hope this information helps in troubleshooting.

salvis’s picture

Very good feedback, thanks!

Please remove the patch in #9 and try this one.

(I hope I can stick with !username_themed if I put it ahead of !username...)

Bilmar’s picture

Hi salvis,

The !username_themed is printing as 'Anonymous' in the emails with the patch at #14, should be printing 'billy'
Anonymous is the name set for Anonymous user at www.example.com/admin/settings/site-information
It seems it is taking the themed username of the anonymous user and not of the new registering account.

salvis’s picture

new registering account

Hmm, I wonder whether that account even exists yet.

With the default implementation of theme_username() you should at least get 'trupal218'. If you have a custom theme, we don't really know what it's doing.

Can you try this in a situation where we know that the account really exists, e.g. for sending the password recovery mail?

Bilmar’s picture


I validated the user, logged in and see the themed username on all pages of my site, then logged out and tested with Request New Password but the email that is sent shows 'Anonymous' =(

salvis’s picture

Please add a line below

  $account = $mail['params']['account'];

to make it look like

  $account = $mail['params']['account'];
  drupal_set_message("name[$account->name], uid[$account->uid]");

Whenever the variables are prepared, you should see a message on the screen. What does it tell you?

Bilmar’s picture


I am getting this output after registering:

- name[trupal218], uid[245]
- A validation e-mail has been sent to your e-mail address. In order to gain full access to the site, you will need to follow the instructions in that message. 

The 'validation link' email shows !username_themed as 'Guest'

And then this message after validating, logging in, logging out, and clicking Request New Password:

- name[trupal218], uid[245] 
- Further instructions have been sent to your e-mail address.

The 'password reset' email shows !username_themed as 'Guest'

'trupal218' is the username
themed username should be 'billy'
once i'm logged in, everywhere on the site (ie contact us block etc) shows 'billy'

I hope this information helps to troubleshoot

Thanks and please let me know what else I can do to help!

salvis’s picture

Status: Needs review » Needs work

Hmm, so we do get what we expect in $account after all — strange...

Now add the following:

  drupal_set_message("themed1[". theme('username', $account) ."]");
  drupal_set_message("themed1+cp'd[". check_plain(theme('username', $account)) ."]");
  drupal_set_message("themed2[". theme_username($account) ."]");
  drupal_set_message("themed2+cp'd[". check_plain(theme_username($account)) ."]");

What do you get?

Bilmar’s picture

hi salvis,

Below is the result from using the above code:

•themed1+cp'd[<a href="http://www.example.com/profile/246" title="View user profile.">billy</a>]
•themed2+cp'd[<a href="/profile/246" title="View user profile.">trupal218</a>]
•A validation e-mail has been sent to your e-mail address. In order to gain full access to the site, you will need to follow the instructions in that message.

The first one looks to print the themed username 'billy' correctly.
Also, 1st bullet (billy of themed1[billy]) and 3rd bullet (trupal218 of themed2[trupal218]) are hyperlinked to "http://www.example.com/profile/246"

The email still shows 'Anonymous'.


roball’s picture

You can get the realname in plain text from the global $user variable like this:

global $user;
$realname = theme('username', $user, array('plain' => TRUE));

No need for check_plain().

salvis’s picture

@roball: Thank you for this piece of information. We're trying to come up with a more general solution that works not just for the Realname module, but adding array('plain' => TRUE) shouldn't hurt. I'll do that in the end, because we should be able to get it to work even without it.

Thank you for the new results, trupal218! We're getting there, slowly but surely...

I think what we want is this:

  drupal_set_message("plain[". strip_tags(theme('username', $account)) ."]");

and ultimately this:

    '!username_themed' => strip_tags(theme('username', $account)),

I'm puzzled why you keep getting 'Guest' or 'Anonymous'. If you still get one of these, try

    '!username_themed' => 'TEST',

just to see if we're looking at the right line of code...

Bilmar’s picture

Hi salvis,

First, I applied the patch at #14 (http://drupal.org/node/792244#comment-2962290) then:


$account = $mail['params']['account'];
drupal_set_message("plain[". strip_tags(theme('username', $account)) ."]");

Message shows: •plain[billy]
Email shows:

&lt;a href=&quot;http://www.example.com/profile/248&quot; title=&quot;View user profile.&quot;&gt;billy&lt;/a&gt;

Changed line '!username_themed' => check_plain(theme('username', $account)),
To '!username_themed' => strip_tags(theme('username', $account)),
Message shows: •plain[billy]
Email shows: billy <= nice!! (I tested this several times)

'!username_themed' => 'TEST',
Email shows: TEST
I'm not sure why the Guest/Anonymous was showing in the email before but I'm not seeing it now.

Thanks for your continuing effort to add this feature to your module!
Let me know if I can do any further testing =)

salvis’s picture

Status: Needs work » Needs review
1.05 KB

Good news, thanks!

So, here's the patch that should finally work. Please give it a try and let us know.

Bilmar’s picture

Status: Needs review » Reviewed & tested by the community

Tested and it works perfectly!

salvis - If you are able to spare some time to look into adding this same feature to Email Confirm module #791606: make !username use themed username which also doesn't print the themed username at the moment, it would be very much appreciated! The maintainer of Email Confirm doesn't seem to be looking into the issue queue, and it be really nice to be able to address the user by First name (my themed username) when sending an email change confirmation.

Thanks so much!

YK85’s picture

awesome! thanks for your hard work! this also helps as a workaround for #761948: Add !realname token to email notifications from core (admin/user/settings)
RTBC and +1 for commit

salvis’s picture

Status: Reviewed & tested by the community » Fixed

Committed to the -dev version — give it up to 12h to be repackaged.

Thanks to all who helped to hammer this out!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Rizhaya’s picture

Status: Closed (fixed) » Active
salvis’s picture

The problem (posted by tobiberlin) is

I installed Realname module and Mail Editor in order to get it working to use user's real names like "Hi Steve Balmer, thank you for you registration...". But when I use !user_themed in a template for the first confirmation (user_register_no_approval_required) there is still the simple Drupal username used (for example "Hi steve456"). When Steve aks for a new password (user_password_reset) he will be addressed with his realname.

Is it not possible to use the realname with the first confirmation email or do I miss something???

Is there anything we can do about this?

tugis’s picture


prakashacharya’s picture

Can you please help me how to accomplish this task. i have installed realname and mail editor 6.x-1.x-dev. i want to include the real name in the email that goes out to the users from user realtionship module. please help me.

manuel.adan’s picture

Try rules module instead. Core user notifications is very basic, and only suitable to use "as is". As far I know, rules can catch user creation event, and do almost anything from that point.

  • salvis committed 64e1e87 on 8.x-1.x
    #792244: Add !username_themed variable.
salvis’s picture