Modifying the $message['key'] variable in hook_mail_alter does not change the mail key

Created on 23 January 2024, 8 months ago
Updated 25 January 2024, 8 months ago

The issue is that $message['key'] is available in the hook_mail_alter hook, but it cannot be changed. It would be beneficial to make this possible, as it could be useful in certain scenarios. For example, when using the Mail System β†’ module, we can configure mail formatter or sender by $message['key'], but I believe this flexibility could be helpful for other modules as well.

MailManager.php

    // Bundle up the variables into a structured array for altering.
    $message = [
      'id' => $module . '_' . $key,
      'module' => $module,
      'key' => $key,
      'to' => $to,
      'from' => $site_mail,
      'reply-to' => $reply,
      'langcode' => $langcode,
      'params' => $params,
      'send' => TRUE,
      'subject' => '',
      'body' => [],
    ];

...

    // Build the email (get subject and body, allow additional headers) by
    // invoking hook_mail() on this module.
    $this->moduleHandler->invoke($module, 'mail', [$key, &$message, $params]);

    // Invoke hook_mail_alter() to allow all modules to alter the resulting
    // email.
    $this->moduleHandler->alter('mail', $message);

    // Retrieve the responsible implementation for this message.
    $system = $this->getInstance(['module' => $module, 'key' => $key]);
πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
MailΒ  β†’

Last updated 21 days ago

No maintainer
Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

  • Issue created by @dejan0
  • Status changed to Needs review 8 months ago
  • The small patch is available here. However, upon further consideration, I'm unsure if it's a good idea to allow this to be changed. Modules typically use it in hook_mail() to filter out their messages. Allowing other modules to change it in between might not be ideal. Let's see what others think.

  • last update 8 months ago
    29,722 pass
  • Status changed to Needs work 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Issue summary should be updated with the standard template with all sections. Even if they don't apply it's easier for committer to see N/A vs it not being there.

    Also will need a failing test to show this issue.

    Thanks for reporting!

Production build 0.71.5 2024