Hourly Limit on Outgoing Emails
Caps how many emails the site can send per hour and stops the rest, which protects your mail account and domain reputation if a form or plugin is abused to send spam.
Opens WPIDE on
Code
$wpide_mail_hourly_cap = 100;
add_filter( 'pre_wp_mail', function ( $return ) use ( $wpide_mail_hourly_cap ) {
if ( null !== $return ) {
return $return;
}
$key = 'wpide_mail_count_' . gmdate( 'YmdH' );
$count = (int) get_transient( $key );
if ( $count >= $wpide_mail_hourly_cap ) {
if ( ! get_transient( $key . '_noted' ) ) {
error_log( sprintf( 'WPIDE: hourly email limit of %d reached; further emails this hour are blocked.', $wpide_mail_hourly_cap ) );
set_transient( $key . '_noted', 1, HOUR_IN_SECONDS );
}
return false;
}
set_transient( $key, $count + 1, HOUR_IN_SECONDS );
return $return;
}, 1 );
Notes
Set the limit above your busiest real hour; blocked emails are reported as failures and noted in the PHP error log once per hour.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.