WordPress.org

Make WordPress Core

Opened 21 months ago

Last modified 15 months ago

#38028 new enhancement

Send emails via an action

Reported by: markcallen Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Mail Keywords: dev-feedback
Focuses: Cc:

Description

There are a number of ways that you may want to interact with core emails in WordPress. To name two

  • Use a different server to send (e.g. to benefit from DKIM/SPF)
  • Use custom templates, HTML, multipart etc.

Currently solutions to achieve these things are not ideal. Pluggable functions leave exposure to other core changes and leave no scope for a 'fallback' to default if it is necessary.

Filters for content are inconsistently (if at all) applied leaving some messages totally inaccessible to developers, save through the wp_mail filter - which is hacky at best (try catching a translated message).

My proposal is relatively simple - send all emails in core through an action hook.

do_action( 'wp_mail_{email_id}', $args );

$args of course would carry the necessary elements for the dynamic parts of a message.

An call add_action can then exactly replace what is being done at present to generate the message and make the wp_mail call.

Any developer that wants to interact with the message can then simply remove the core action and add their own - to alter the message and/or send using an alternative mechanism to wp_mail, falling back to it if necessary. Possibilities for developers are then endless.

wp_mail stays in tact and as the hook call replaces current functionality there is no issue with backward compatibility - all existing filters can stay in place.

Attachments (1)

wp-mail.38028.diff (2.8 KB) - added by markcallen 21 months ago.
Retrieve password email triggered through action

Download all attachments as: .zip

Change History (5)

#1 follow-up: @johnbillion
21 months ago

  • Version 3.3 deleted

Better control over notifications is an area I've wanted to work on for a while now, but I haven't managed to find the time for it.

Here's a doc I created which lists all of the instances where WordPress sends an email, and the related hooks and filters: https://gist.github.com/johnbillion/0a48021de5510c41d517 . These would need to be taken into account for backwards compatibility.

#2 in reply to: ↑ 1 @markcallen
21 months ago

Replying to johnbillion:

Here's a doc I created which lists all of the instances where WordPress sends an email, and the related hooks and filters: https://gist.github.com/johnbillion/0a48021de5510c41d517 . These would need to be taken into account for backwards compatibility.

This is a useful list that clearly shows the inconsistent approach to how emails are filtered and sent. Using filters to disable actions should not, IMO, be the best approach.

My proposal, I believe, leaves everything in place so backward compatibility is assured.

It simply introduces a specific hook per email which can be left alone for everything to stay as is, removed to disable the message, or replaced for developers to do with as they wish.

@markcallen
21 months ago

Retrieve password email triggered through action

#3 @markcallen
21 months ago

  • Keywords dev-feedback added

The attached patch covers one of the wp_mail calls in core - it serves to demonstrate

  • Back-compat is covered, it does the same thing when left alone.
  • The default action can be adjusted using the pre-existing filters
  • The action could be removed, preventing the wp_mail call and remove the need for any of the numerous hacks out there to stop mails being sent by WordPress.
  • The action can be replaced - giving a developer complete freedom to use wp_mail (primarily or as a fallback), or any number of third-party email services (e.g. SMTP or API), or even just to a log file for development purposes.

I will patch the rest of the calls to wp_mail if this is something that can be committed.

I'd love to hear any other ideas that give both, full back-compat and total freedom to developers to send emails how they want.

#4 @markcallen
15 months ago

I understand that this falls within the notifications API proposal (https://make.wordpress.org/core/2016/10/03/feature-project-proposal-notifications-api/).

This solution - perhaps with some naming changes - manages this. It's backward compatible, can be used to send notifications using any service, not just email and in my opinion makes improves the workflow dramatically. Sending notifications is an action and should be triggered as such.

It's been six month since this was touched. Can we do anything to move it along?

Note: See TracTickets for help on using tickets.