Following on from my testing web forms videos released earlier this week, I received a follow-up question
The question was, how do I know it worked and delivered? Now, for me, this isn’t an issue. They end up in my email box, and that’s all I need to worry about, but how would I approach this as a host or developer?
I would intercept the email using a known phrase in this example, we use “WebForm Testing X5854” unless your client is a web form tester it’s very unlikely this phrase would be in the email for any legitimate reason, and as such, we look and filter emails with that within the body and if that matches we use the wp_mail filter to change the recipient to our agency email address
The snippet below is all you need; change the intercept string (I’d recommend a unique one for each client) and set the snippet in your config plugin or functions.php or snippet plugin, and away you go.
<?php
function intercept_email_and_modify_recipient( $args ) {
// Define the string to search for in the email content
$string_to_intercept = 'WebForm Testing X5854'; // Change to what you actually are looking for as random as possible!!!
$new_email_address = '[email protected]';
// Check if the email body contains the specific string
if ( strpos( $args['message'], $string_to_intercept ) !== false ) {
error_log('Email intercepted: Changing recipient to ' . $new_email_address);
// Change the recipient to the new email address
$args['to'] = $new_email_address;
}
return $args;
}
add_filter( 'wp_mail', 'intercept_email_and_modify_recipient' );
PHPYou can watch the follow-up video to see this in action here