Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata to mailable view data #53042

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

TobMoeller
Copy link
Contributor

Background

While developing a package that filters outgoing emails by recipient using the MessageSending event, I noticed a discrepancy between notifications and mailables. Notifications include metadata like the notification class in the viewData (which is also provided to the event). However, when sending a Mailable, this functionality is not present and it is impossible to identify the originating class from the event.

What is currently present for notification context:

/**
 * Get additional meta-data to pass along with the view data.
 *
 * @param  \Illuminate\Notifications\Notification  $notification
 * @return array
 */
protected function additionalMessageData($notification)
{
    return [
        '__laravel_notification_id' => $notification->id,
        '__laravel_notification' => get_class($notification),
        '__laravel_notification_queued' => in_array(
            ShouldQueue::class,
            class_implements($notification)
        ),
    ];
}

Proposed Changes

This pull request introduces an additionalMessageData method to the Mailable class to enhance the provided viewData, mirroring the functionality present in the MailChannel class used for notifications. The aim is to provide consistent metadata in the MessageSending event across both notifications and mailables. As this is also passed to the MessageSending event, this enables event listeners to identify the message source.

Considerations

This PR does not fully mimic the notifications metadata. Currently i have only included the Mailable class to the meta data as I am not aware of a similar id attribute on mailables and checking for the implementation of the ShouldQueue interface might not be enough to identify if the mailable was queued (due to ways to queue a mailable without it's implementation). I might be wrong, but the "__laravel_notification_queued" information on notifications might also not be 100% accurate depending on the way it is queued, so it might be ok to include the same information for mailables for consistency?

@taylorotwell taylorotwell merged commit bc5bd66 into laravel:11.x Oct 7, 2024
33 checks passed
@TobMoeller TobMoeller deleted the add_mailable_metadata branch October 13, 2024 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants