Has anyone developed an extension to handle null values in tokens?

For example:

Sending an email to a contact with no first name:

Dear {first.name}, will end up as -> Dear ,

It appears that previously in 3.3 it was possible to use {first.name | default text}

Additionally a long while ago there was some discussion about reviving this behaviour.

Following this there was this post about how to change the behaviour site wide, however to enable more personalised emails (both in terms of the sender and recipient) would be great to allow for this to be done on a per email basis as well.

Following that I can only spot this SE question referencing the issue last year.

4 Answers 4


We used the Dave's Blog https://civicrm.org/blog/dave-greenberg/civimail-how-to-add-default-values-for-empty-tokens

The blog provides a sample code and the hook which need to be used. We added the same tokenValues() hook to the extension and it works great in replacing all the empty token values with the one specified in the hook.

  • 1
    if someone wants the code just sing out.
    – petednz - fuzion
    Apr 20, 2018 at 2:17
  • Is the extension you made released publicly?
    – Laryn
    Feb 26, 2019 at 16:19

If you enable the Smarty template engine for mails with define('CIVICRM_MAIL_SMARTY', 1); you can use it to to handle defaults on a per mailing basis: {capture assign=first_name}{contact.first_name}{/capture} Dear {$first_name|default:Friend},

see also: https://docs.civicrm.org/user/en/latest/common-workflows/tokens-and-mail-merge/#smarty)

  • THanks Thomas - good to know. we have gone ahead and done this via an extension now (though it doesn't provide a UI for setting this), but I will aim to add a blog about it in case it helps others
    – petednz - fuzion
    Mar 13, 2018 at 18:43
  • Really helpful answer. Thank you. Certainly seems the easiest way to set up default replacement values.
    – Ben
    Feb 26, 2023 at 15:21

One workaround is to define your own token that handles nulls and defaults as you want. But I agree it would be useful to have a non-code way to implement defaults.


Since people are looking at this question, it's worth noting that you don't need to enable Smarty system-wide to do this for greetings. CiviCRM 4.5+ automatically handles Smarty in the greetings fields, see this blog post: https://civicrm.org/blog/antrik/getting-started-with-new-name-and-greetings-features-in-45

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.