How to provide a specific node context to allow tokens to work in a custom email?

Created on 8 August 2024, 4 months ago

Hi, I need some help to understand if this module can solve my use case, or if I need to find some other alternative.

Use case description (simplified)

  1. Every time a user subscribes to a simplenews newsletter,
  2. an email is sent to the user who just signed up
  3. the email contains a list of "protected" links to a series of images to download, within a certain period of time, after which such links will no longer be valid.

I managed to implement points 1 and 2 without using the rules module, creating a custom email through sympony_mailer module, which is sent inside a hook_simplenews_subscribe() (so, every time a user completed the subscription).

For point 3 I created a node (it will always be this one, the same for all users) to which I attached a field_images (multiple) setted as Protected files as Upload destination, as indicated in the instructions of this module.

Viewing this node, the attached images are actually rendered with src like "/protected-download/protected/66be9XXX/TvxjllFNxVrSuXRZIt7r23OzFiTtoXEXtt1g9rrq9K8/file-name.jpg" so I assume this module is set correctly.

I also tried using the following tokens directly in the body of the node itself (the token_filter module must be active) and they work perfectly:

file:protected-download-url token: WORKS!

[node:field_images:0:entity:protected-download-url]


file:protected-download-expire token: WORKS!

[node:field_images:0:entity:protected-download-expire]

The question is:
above tokens obviously work because we are in the context of the node, but how can I make them work in my custom mail where the context of that node does not exist?

Am I using the right approach?
How can I provide the context of that specific node (with specific node ID) in my mail?
Do I have to create a custom token where I load the node and pass it somehow to the mail as a context to then make the protected_download tokens work?

Thank you for your help: any suggestions are appreciated.

💬 Support request
Status

Active

Version

2.0

Component

Documentation

Created by

🇮🇹Italy mxt Milan

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

Comments & Activities

  • Issue created by @mxt
  • 🇮🇹Italy mxt Milan

    Do I have to create a custom token where I load the node and pass it...

    Yes, this seems like the correct approach: I created a custom "global" token that returns the specific node object containing the protected images everywhere, to which you can then append the protected_download specific tokens, something like this:

    
    file:protected-download-url token:
    [my-global-token-type:my-specific-node:field_images:0:entity:protected-download-url]
    
    file:protected-download-expire token:
    [my-global-token-type:my-specific-node:field_images:0:entity:protected-download-expire]
    
    

    And you can put above tokens wherever you want, including the email automatically sent after each newsletter subscription.

    One thing I still have to figure out is how to set a custom expiration date, because the one printed by the token seems to be predefined and I haven't yet found a way to change it at will (but this will eventually be the subject of another issue)

Production build 0.71.5 2024