Custom tokens are not evaluated during CiviMail mailings.
Reproduction steps
Add example code from Defining tokens documentation to define two custom tokens: {profile.viewUrl} and {profile.viewLink}. Cleanup caches.
Create a CiviMail mailing and insert the new tokens then submit the mailing.
Current behaviour
Resulting email sent by the background mailing job does not include either of the custom tokens. Emails containing the custom tokens are evaluated properly when sent as a CiviMail test or via a send email action for a contact.
Expected behaviour
CiviMail mailings must evaluate custom tokens.
Environment information
CiviCRM: 5.58.0
PHP: 7.4
CMS: Both WordPress 6.1.1 and Drupal 7.94
Database: MySQL 5.7
Web Server: Apache
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Child items ...
Show closed items
Linked items 0
Link issues together to show that they're related.
Learn more.
Same issue. Sending an e-mail with custom tokens triggered by a civiRule actually occurs to return an error.
[warning] Deprecated function CRM_Utils_Token::getTokenDetails, use If you hit this in mailing code you should use flexmailer - otherwise use the token processor.CRM_Core_Error::deprecatedFunctionWarningCRM_Utils_Token::getTokenDetailsCRM_Pdfapi_Pdf::create::civicrm_api3_pdf_createArray( [civi.tag] => deprecated)
Trying to flush cache shows this error:
PHP Fatal error: Uncaught Error: Class 'Civi\FlexMailer\Services' not found in /var/www/vhosts/domain.com/httpdocs/sites/all/modules/civicrm/ext/flexmailer/flexmailer.php:103
As for your primary issue, I don't think that's a core issue, I think it's a PDF API extension issue. Try applying this patch? extensions/pdfapi!41 (merged)? Also note a new version was released yesterday, so apply this patch atop the new version.
I am not using CiviRule or the PDF API extension, and there are no errors in the error log for the CiviMail issue I submitted. The problem described by @GuillaumeSorel is different, and should be tracked as a separate issue.
Thanks, @totten. The only difference might be that my code is implemented in a custom Drupal module (and WordPress plugin), and not in a CiviCRM extension. Could that be a factor?
@marty That does indeed make a difference! tldr: Try running the background job another way.
In my testing of a D7 module... The "Preview" and "Send test" seem OK regardless. The trouble comes when you submit and then run the background job. It depends on how you fire the background job. Compare:
cv api --level=full -U admin Job.process_mailing #Badcv api --level=cms-full -U admin Job.process_mailing #OK
The key facts here are that (1) hook_container runs during system bootstrap and (2) these two examples use different bootstrap processes. Loosely speaking:
With --level=full, it loads Civi then Drupal. (Older protocol)
With --level=cms-full, it loads Drupal then Civi. (Newer protocol)
There are many other ways to fire Job.process_mailing, but they generally fall into those same two buckets. I spot-checked a few more.
So I suggest running your background job in some other way.
The issue here is not really specific to CiviMail or tokens, and it's not really worth the effort to fix the old boot protocol. It's more about handling the transition. For example:
More of the cv subcommands should default to --level=cms-full.
APIv3 Explorer should encourage civicrm/ajax/rest instead of extern/rest.php.
Reading the above there seems to have been lots of factors in play but it kind fizzled out without clatifying the problem. I'm going to close this as it is not replicable / solvable based on the informaiton here (and it is nearly a year old) but re-open or open an issue with steps to replicate if there is still an issue