I am trying to put some logic into user registration welcome emails. Simple "if-then" logic, based on tokens.

The tokens I use show up as expected, but both twig and php code gets sent as plain text.

Can I somehow make twig or php code execute before the email is sent to the new user?

UPDATE, to make the question more specific:

I have token module enabled.

I am trying to add this logic to the welcome email sent to new users at "/admin/config/people/accounts":

{% if [user:field_marketing_package] == "Yes" %}
<p>Marketing selected</p>
{% endif %}

The [user:field_marketing_package] field is filled out at user registration.

I would like to add logic like this. If I have the above twig code in the message, it just gets sent as plain text and does not execute.

Twig or php or any other solution is fine as long as I can include the token in the if statement.

Can you point me to the right direction?

  • 2
    Can you elaborate more on what the logic will do?
    – No Sssweat
    Feb 3, 2019 at 16:56
  • Sure, thanks for your comment :) It will calculate a price based on selected values, that the user enters on the registration form.
    – ventura
    Feb 3, 2019 at 19:39
  • Could you please update your question, provide some sample of your Twig and PHP code and/or token and try to explain this to someone who doesn't know your project, your specs, your modules and your config?
    – leymannx
    Feb 3, 2019 at 21:56
  • Wrote an update. Hope it clarifies what I'm trying to do. Thanks for the replies.
    – ventura
    Feb 3, 2019 at 23:36
  • I didn't test it though, but I'd look into hook_mail() api.drupal.org/api/drupal/core%21core.api.php/function/… or hook_mail_alter() api.drupal.org/api/drupal/core%21core.api.php/function/… for adding arbitrary information to automatically generated emails. User notifications have the account in question in their parameters. So you can access this information through the $params argument/'params' message array key. Feb 4, 2019 at 8:48

1 Answer 1


Pretty ugly, but I have a solution. Please post if you have a better one that can be applied for more use cases.

So I installed the https://www.drupal.org/project/computed_field module.

I added a hidden computed filed to my user registration form with my logic.

Then I used the token module to include the value of that computed field in my user welcome email.

It does the job, but again, it is pretty ugly. Hopefully this helps someone until someone posts a more direct solution.

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.