Hook before WP sends email

Joy

(@joyously)

You can see all the hooks with the word “mail” here: https://developer.wordpress.org/?s=mail&post_type%5B%5D=wp-parser-hook

Hi Joy, yes — I admit that I haven’t tried every single one, but I did look through the ones that appeared promising and they didn’t fire. I think they’re mostly used to verify things when users change their email.

I assume there is *some* way to do this, and I’m hoping that someone who knows how can tell me what I missed 🙂

Joy

(@joyously)

It looks to me like wp_mail filter is the one you want, since phpmailer is lower level and both are pluggable.
https://developer.wordpress.org/reference/functions/wp_mail/

@joyously, does this hook actually fire for you? If so, what kind of mail are you sending. When I tried it, it never fired unless my code was explicitly. calling wp_mail().

I’m under the impression that wordpress actually calls the php mail() function rather than wp_mail() but I could be confused about this.

Joy

(@joyously)

I’ve never used any of the mail functions or hooks.

Joy

(@joyously)

WP 5.7 added a new filter called pre_wp_mail so you can know if the pluggable function was replaced.
For more information on this change, see #35069 on Trac.
(I found this in https://make.wordpress.org/core/2021/02/23/miscellaneous-developer-focused-changes-in-wordpress-5-7/ )

If WordPress bypassed wp_mail(), then none of the available SMTP plugins would work. So somewhere in your WP site code, a plugin/theme is directly calling the PHP mail() function. That’s IMO bad coding, and it would be worth locating and then removing whatever plugin/theme is doing that.

The phpmailer_init hook passes the PHPMailer instance by reference…it’s not a typical hook. I use it on a regular basis to replace the #&*$^! wordpress@wpdomain.com From: address with the site email address, and to set the sendmail -f parameter to that email address. I also use it on occasion to set up SMTP access to the likes of Amazon SES.

@diondesigns, you are right. I tested this with wordpress itself (password resets and so on) and the wp_mail hook did fire then. So it appears that I’m dealing with some no-so-well behaved code. Thanks for helping me clear up what was happening.

And @joyously, thanks for that info on 5.7. That’s very interesting.



Source link