Support » Developing with WordPress » Hook before WP sends email

  • Resolved Mike Witt

    (@mike80222)


    I’m looking for a hook I can use to examine the destination address of an email before it’s sent. For example, it appears that phpmailer_init fires before each outgoing email, but that appears to give me everything except the “To” address.

    And (I just realized) phpmailer_init doesn’t fire for all outgoing email.

    Is there someplace I can hook in to get the To address for any email sent from wordpress, or are there per-plugin considerations?

    Many thanks!

    -Mike

    • This topic was modified 1 year, 5 months ago by Mike Witt.
Viewing 8 replies - 1 through 8 (of 8 total)
  • You can see all the hooks with the word “mail” here: https://developer.wordpress.org/?s=mail&post_type%5B%5D=wp-parser-hook

    Thread Starter Mike Witt

    (@mike80222)

    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 🙂

    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/

    Thread Starter Mike Witt

    (@mike80222)

    @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.

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

    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/ )

    Dion

    (@diondesigns)

    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.

    Thread Starter Mike Witt

    (@mike80222)

    @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.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Hook before WP sends email’ is closed to new replies.