When working with WordPress email functions, you might come across situations where your site (or a plugin) tries to send an email without a proper recipient. This can lead to errors, wasted server resources, or even deliverability issues.
Thankfully, WordPress provides a handy filter called pre_wp_mail
that lets you intercept outgoing emails before they’re sent. In this tutorial, we’ll walk through a practical code snippet that blocks emails with empty or invalid recipients.
The Code Snippet
add_filter('pre_wp_mail', function($null, $atts) {
// If "to" is missing or empty, abort sending
if (empty($atts['to'])) {
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('Blocked email attempt: "to" field empty');
}
return false; // prevents wp_mail from sending
}
// If "to" is array, check contents
if (is_array($atts['to'])) {
$valid_to = array_filter($atts['to'], function($email) {
return !empty($email) && is_email($email);
});
if (empty($valid_to)) {
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('Blocked email attempt: all recipients invalid/empty');
}
return false;
}
}
return $null; // let normal mail flow if valid
}, 10, 2);
Step-by-Step Explanation
1. Hook into pre_wp_mail
The pre_wp_mail
filter runs before WordPress actually sends an email using wp_mail()
. By hooking into it, we can decide whether to let the email go through or block it.
add_filter('pre_wp_mail', function($null, $atts) { ... }, 10, 2);
$null
→ Default value (we return this if everything’s fine).$atts
→ The email parameters (liketo
,subject
,message
, etc.).
2. Check if the “to” field is empty
If the recipient field is missing or empty, we stop the email from sending.
if (empty($atts['to'])) {
error_log('Blocked email attempt: "to" field empty');
return false;
}
error_log()
only runs ifWP_DEBUG
is enabled — so you’ll see logs in development, but not in production.return false;
tells WordPress to cancel the email.
3. Validate email addresses if “to” is an array
WordPress lets you send emails to multiple recipients using an array. This section ensures that each email is valid and not empty.
if (is_array($atts['to'])) {
$valid_to = array_filter($atts['to'], function($email) {
return !empty($email) && is_email($email);
});
if (empty($valid_to)) {
error_log('Blocked email attempt: all recipients invalid/empty');
return false;
}
}
- Uses
is_email()
to validate addresses. - If no valid recipients remain, the email is blocked.
4. Let valid emails send normally
If the checks pass, we simply return $null
so WordPress continues with its default mail flow.
return $null;
Why This is Important
- Improves deliverability → Prevents your mail server from attempting to send invalid messages.
- Reduces errors → Avoids “Invalid recipient” warnings.
- Security safeguard → Blocks poorly coded plugins from spamming with blank recipients.
- Debugging friendly → Uses
error_log()
so you can trace blocked emails in development.
Final Thoughts
This simple filter makes your WordPress email system more robust by ensuring only valid messages are sent. It’s a lightweight, performance-friendly solution that you can drop into your theme’s functions.php
file or a custom plugin.