Intercepting wp_mail() to view contents

I’m looking for a way to hook into the mail function before it sends so I can var_dump the output. Are there any action hooks with the mail message that I can hook into? I’m having trouble finding the wp_mail() function in core. Any other methods of debugging mail output would be greatly appreciated as well.

1 Answer
1

The first link on Google is to https://developer.wordpress.org/reference/functions/wp_mail/ which says that wp_mail is in wp-includes/pluggable.php

It also has the full function source code showing that the first active line of the function is:

$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );

… which suggests that if you hook into the filter you can capture all of those fields.

Additionally as it’s a pluggable function you could also replace it with your own function doing whatever you want with the messages.

Leave a Comment