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

- Type: PHP
- Runs: Run everywhere
- Category: [Email](https://wpide.com/library/category/email/)
- Tags: email, security, rate-limit
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/email-hourly-send-limit/

## Code

```php
$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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Hourly Limit on Outgoing Emails" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
