hook_mail_alter() implementations aren't invoked early enough

Created on 7 August 2023, over 1 year ago
Updated 28 November 2023, about 1 year ago

Problem/Motivation

In our project, there is a hook_mail_alter implementation which modifies (actually creates) the from address in the legacy $message array. The emails sent by the "sender" module is handled by the LegacyEmailBuilder plugin class (luckily), but this plugin does not sets the from address at all.

My problem consists of two parts:

  1. Since mail_alter implementations are only invoked in LegacyEmailBuilder and during the build phase, the initially constructed Email instance only contains the initial $message array. This makes impossible to access the altered metadata to all email processor (email adjuster) plugin.
  2. Shouldn't something set the from address set in messages array by default in Drupal Symfony Mailer? Or reply-to?

Steps to reproduce

  1. Send email with my_module (utilizing hook_mail).
  2. In a mail alter hook, set from to an arbitrary email address.

Expected: From email set in the alter hook is available to email adjuster plugins.
Actual: Only the original from email (if any) is available to email adjuster plugins.

Proposed resolution

A: Invoke hook_mail (if any) and hook_mail_alter hooks in MailManagerReplacement::mail() instead of LegacyEmailBuilder.
B: If not invoking mail alters for non-legacy emails is by design, invoke both hooks as part of LegacyEmailBuilder::fromArray().
C: ??

Remaining tasks

TBD.

User interface changes

Nothing.

API changes

TBD.

Data model changes

Nothing.

πŸ› Bug report
Status

Closed: works as designed

Version

1.0

Component

Code

Created by

πŸ‡­πŸ‡ΊHungary huzooka Hungary πŸ‡­πŸ‡ΊπŸ‡ͺπŸ‡Ί

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

Comments & Activities

  • Issue created by @huzooka
  • πŸ‡¬πŸ‡§United Kingdom adamps

    I don't yet see any bug.

    • DefaultsEmailAdjuster sets the sender address. If nothing sets a from, Symfony will set it to match the sender.
    • LegacyEmailBuilder::build() calls hook_mail() then hook_mail_alter(). This is by design only for LegacyEmailBuilder - in other cases we don't want to call hook_mail() and we therefore can't call hook_mail_alter() because the expected values to alter won't be present.
    • Email adjuster plug-ins can set a weight. If they set a weight greater than LegacyEmailBuilder then they will see the results of the above two hooks.
  • πŸ‡³πŸ‡±Netherlands wilfred waltman

    I kind of experience the same problem, or at least I think. It occurred during the migration to Drupal 10 while using version 1.4.0 of Drupal Symfony Mailer.

    When I do not alter the From-header in the hook_mail of my custom module, the site name and mail are implemented in the From header.
    As soon as I try to alter the From header in the hook_mail, it results in me getting {{ getSender(message) }} as the From name.

    I added a screenshot for clarification.

  • Status changed to Fixed about 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom adamps
  • Status changed to Active about 1 year ago
  • πŸ‡³πŸ‡±Netherlands wilfred waltman

    @adamps, can you tell me what is fixed? Or how I can fix it myself?

    Because I still experience the issue.

  • πŸ‡ͺπŸ‡ΈSpain rcodina Barcelona

    @AdamPS I migrated to Symfony mailer module from Swiftmailer while upgrading Drupal to 10.1 from 9 version. In my site I had a hook_mail_alter to style the register email to be an HTML mail using a Twig template. Now, the hook_mail_alter isn't called. What should I do to recover this feature? It's an strict requirement from my customer.

  • πŸ‡ͺπŸ‡ΈSpain rcodina Barcelona

    I finally migrated to SMTP + Mime mail and I removed Symfony mailer to preserve hook_mail_alter features.

  • Status changed to Closed: works as designed about 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom adamps

    If fixed is confusing, then let's call it "works as designed". @Wilfred Waltman and @rcodina please see #2 where I explained that everything seems to be working exactly as I would expect.

    Or how I can fix it myself?

    See bullet 3 in #2. You can set the weight for your EmailAdjuster to control whether you run before or after LegacyEmailBuilder.

Production build 0.71.5 2024