Tokens containing HTML in notification emails show HTML tags in emails

Created on 15 June 2022, about 3 years ago
Updated 17 December 2024, 9 months ago

Problem/Motivation

I am using tokens inside emails such as the Registration Reminder email. These tokens come from custom fields I have added to the registration process on the Event Series entity, which can contain full HTML. Specifically, I have a field custom_registration_reminder that I place inside the Registration Reminder email as a token [eventseries:custom_registration_reminder]. This is so event organizers can place a unique registration reminder inside the reminder emails.

My Registration Reminder email set on /admin/structure/events/registrant/settings looks like this:

This is a reminder for your event.

[eventseries:custom_registration_reminder]

I am using Mime Mail module to send emails from my site in HTML format.

When Registration Reminder emails arrive, the HTML from the [eventseries:custom_registration_reminder] token is visible inside the email, not being translated into HTML. For example:

This is a reminder for your event.<p>Custom message should be here.</p>

I would like these emails to parse the HTML from the token.

Steps to reproduce

Add a custom "Text, (formatted, long)" field to the Event Series entity.

Have the content from that field print out in the Registration Reminder email set at /admin/structure/events/registrant/settings

Create an Event Series with content in the new field.

Trigger the Registration Reminder email to be sent and see that the HTML is not formatted properly.

Proposed resolution

I have success getting the formatting correct by changing code inside parseTokenizedString() in NotificationService.php and using Token::replacePlain() instead of Token::replace()

    return $this->token->replace($this->token->replace($string, $data), $data);

to:

    $tokenized_data = $this->token->replacePlain($string, $data);
    return $this->token->replacePlain($tokenized_data, $data);

I am not sure that this has no repercussions elsewhere. Or maybe my case is very unique. Any advice is appreciated.

✨ Feature request
Status

Needs review

Version

2.0

Component

Recurring Events Registration (Submodule)

Created by

πŸ‡¨πŸ‡¦Canada endless_wander

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The patch #9 solved the problem here.

  • πŸ‡ΊπŸ‡ΈUnited States phillamb168

    The patch in #9 partially solved my issue. However I'm now running across the following behavior:
    Config:
    registration_notification:
    enabled: true
    subject: "You've Successfully Registered"
    body: "Your registration for [eventseries:title] was successful.\r\n\r\n[eventseries:field_revent_confirmation_text]"

    registration_reminder:
    enabled: true
    subject: 'Upcoming Event Reminder'
    body: '[eventseries:reminder_message]'

    Patch from #9 applied.

    Behavior:
    Tokens are correctly replaced for both emails.

    Registration reminders are sent correctly as full html.

    Registration notifications have their HTML stripped.

    Any help would be greatly appreciated.

  • πŸ‡ΊπŸ‡ΈUnited States phillamb168

    After some weekend work I still couldn't figure out why the token replacement would break on reminder vs. registration. If I used replacePlain() it would work for one but not the other, and if I used replace() it would work for the reverse. Then I realized I had different token names in place for each, so I just modified the patch for a customized if statement, looking at the name of the token and performing the appropriate replace function, e.g.:

    diff --git a/modules/recurring_events_registration/src/NotificationService.php b/modules/recurring_events_registration/src/NotificationService.php
    index ac30381..64798fc 100644
    --- a/modules/recurring_events_registration/src/NotificationService.php
    +++ b/modules/recurring_events_registration/src/NotificationService.php
    @@ -431,7 +431,15 @@ class NotificationService {
         ];
         // Double token replace to allow for global token replacements containing
         // tokens themselves.
    -    return $this->token->replace($this->token->replace($string, $data), $data);
    +    // Check to see if the string contains 'reminder_message'. If so, use replacePlain.
    +    // if not, use double replace as before.
    +    // This is to avoid HTML entity encoding of the reminder message.
    +    if (strpos($string, '[eventseries:reminder_message]') === false) {
    +      return $this->token->replace($this->token->replace($string, $data), $data);
    +    }
    +    // Use replacePlain for non-HTML strings to avoid HTML entity encoding.
    +    $tokenized_data = $this->token->replacePlain($string, $data);
    +    return $this->token->replace($tokenized_data, $data);
       }
    
       /**
    
Production build 0.71.5 2024