Halloween Sale 15% off Ends in See the plans
PHP

Reply-To Address Rules by Email Subject

Sets a different Reply-To address depending on the email's subject, so replies to order, support or form emails reach the right team automatically.

Don’t have WPIDE yet? Install it on your site

Code

// Text found in the subject (case-insensitive) => Reply-To address.
$wpide_reply_rules = array(
    // 'order'   => '[email protected]',
    // 'support' => '[email protected]',
);

add_filter( 'wp_mail', function ( $atts ) use ( $wpide_reply_rules ) {
    $headers = is_array( $atts['headers'] ) ? implode( "\n", $atts['headers'] ) : (string) $atts['headers'];

    if ( false !== stripos( $headers, 'Reply-To:' ) ) {
        return $atts;
    }

    foreach ( $wpide_reply_rules as $needle => $address ) {
        if ( false !== stripos( (string) $atts['subject'], $needle ) && is_email( $address ) ) {
            if ( is_array( $atts['headers'] ) ) {
                $atts['headers'][] = 'Reply-To: ' . $address;
            } else {
                $atts['headers'] = trim( $headers . "\nReply-To: " . $address );
            }
            break;
        }
    }

    return $atts;
} );

Notes

Add your subject-to-address rules at the top (the first match wins); emails that already have a Reply-To are left alone.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.