# Route Contact Form 7 Emails by Department

> Sends each Contact Form 7 message to the right inbox based on the department or topic the visitor picked, instead of one shared address.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: contact-form-7, notifications, routing
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/cf7-route-by-department/

## Code

```php
$wpide_cf7_route_field = 'menu-department';
// Option value => recipient address.
$wpide_cf7_routes = array(
    // 'Sales'   => 'sales@yourdomain.com',
    // 'Support' => 'support@yourdomain.com',
);

add_filter( 'wpcf7_mail_components', function ( $components, $contact_form = null, $mail = null ) use ( $wpide_cf7_route_field, $wpide_cf7_routes ) {
    $submission = class_exists( 'WPCF7_Submission' ) ? WPCF7_Submission::get_instance() : null;

    if ( ! $submission || ( $mail && method_exists( $mail, 'name' ) && 'mail' !== $mail->name() ) ) {
        return $components;
    }

    $choice = $submission->get_posted_data( $wpide_cf7_route_field );
    $choice = is_array( $choice ) ? reset( $choice ) : (string) $choice;

    if ( isset( $wpide_cf7_routes[ $choice ] ) && is_email( $wpide_cf7_routes[ $choice ] ) ) {
        $components['recipient'] = $wpide_cf7_routes[ $choice ];
    }

    return $components;
}, 10, 3 );
```

## Notes

Add your choices and addresses to the map at the top (unknown choices keep the form's own recipient); CF7 pipes (Label|email) are an alternative that keeps addresses inside the form.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Route Contact Form 7 Emails by Department" 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/
