# Filtering incoming e-mail messages by IP address

> Find a sender's IP address in the message headers and create a cPanel email filter that blocks or redirects mail from it.

Source: https://helpcenter.ruachost.com/email/cpanel-email/filtering-incoming-e-mail-messages-by-ip-address/

Sometimes you may want to block or redirect messages coming from specific IP addresses. cPanel’s **Email Filters** feature allows you to create rules that process incoming mail based on the sender’s IP.

## Step 1: Determine the IP Address

- Every e‑mail contains **headers** that show its origin.
- Look for the **Received:** header in the raw message.
- Identify the IP address you want to filter.
- For instructions on viewing headers, check your mail client’s documentation.

## Step 2: Create the Filter

1. **Log in to cPanel.**
2. On the **Tools page → Email section**, choose one of the following:

   - **Email Filters** → create a filter for a specific account.
   - **Global Email Filters** → create a filter for all accounts under the domain.
3. Click **Create a New Filter**.
4. Enter a **Filter Name**.
5. Under **Rules**:

   - First box → **Any header**.
   - Second box → **matches regex**.
   - Third box → enter a regular expression for the IP:

     - Single IP:

       ```
       ^xxx\.xxx\.xxx\.xxx$
       ```

       *(replace* `xxx.xxx.xxx.xxx` *with the actual IP)*
     - Range of IPs (last octet 0–255):

       ```
       ^xxx\.xxx\.xxx\.[0-9]{1,3}$
       ```
6. Under **Actions**, choose what to do with matching messages:

   - Discard message.
   - Redirect to another address.
   - Move to a folder.
   - Flag as spam.
7. Click **Create**.

## Step 3: Test the Regex

- Visit <https://regex101.com/> to test your expression before applying.
- Ensure the regex is not too broad, otherwise legitimate mail may be filtered.

> **Note:**
- Filtering by IP is powerful but risky because many providers use shared IPs.
- Always confirm the IP belongs to the unwanted sender before blocking.
- Filters take effect immediately after creation.
