• Resolved nativebreed

    (@nativebreed)


    I have written the code below to change form notification email based on the option selected in a field. I have debugged the code and I can see from a log file that the email returned by the function changes based on the option selected. Why is WPForm not sending to the selected email?

    Please help.

    define( 'MYFC_FORM_ID', 1 );
    define( 'MYFC_SELECT_FIELD_ID',3 );
    define( 'MYFC_DEFAULT_NOTI_EMAIL', 'c@test.com' );
    
    function myfc_get_email_routes() {
        return array(
            'Text A' => 'a@test.com',
            'Text B' => 'b@test.com',
        );
    }
    
    add_filter( 'wpforms_entry_email_atts', 'myfc_dynamic_email_routing', 10, 5 );
    
    function myfc_dynamic_email_routing( $email, $fields, $entry, $form_data, $notification_id ) {
        if ( (int) $form_data['id'] !== MYFC_FORM_ID ) {
            return $email;
        }
        $selected = isset( $fields[ MYFC_SELECT_FIELD_ID ]['value'] )
            ? trim( $fields[ MYFC_SELECT_FIELD_ID ]['value'] )
            : '';
        $routes        = myfc_get_email_routes();
        $routed_email  = MYFC_DEFAULT_NOTI_EMAIL;
        if ( isset( $routes[ $selected ] ) ) {
            $routed_email = $routes[ $selected ];
        }
        $email['address'] = $routed_email;
        return $email;
    }

    Thank you.

    • This topic was modified 3 days, 20 hours ago by nativebreed.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter nativebreed

    (@nativebreed)

    Issue resolved. The issue was that my function was returning a string instead of an array.

    Below is the final working code for anyone that needs it.

    BTW, I also added, in the final version, an option to set notification_id to apply the routing to – I don’t think this is not necessary – but ….

    define( 'MYFC_FORM_ID', 1 );
    define( 'MYFC_SELECT_FIELD_ID',3 );
    define( 'MYFC_DEFAULT_NOTI_EMAIL', 'c@test.com' );
    
    // Optional: set to an integer to restrict email routing to one specific notification. Leave undefined (or set to 0) to apply routing to ALL notifications on the form.
    define( 'MYFC_NOTIFICATION_ID', 1 );
    
    function myfc_get_email_routes() {
        return array(
            'Text A' => 'a@test.com',
            'Text B' => 'b@test.com',
        );
    }
    
    add_filter( 'wpforms_entry_email_atts', 'myfc_dynamic_email_routing', 10, 5 );
    
    function myfc_dynamic_email_routing( $email, $fields, $entry, $form_data, $notification_id ) {
        if ( (int) $form_data['id'] !== MYFC_FORM_ID ) {
            return $email;
        } 
        if ( defined( 'MYFC_NOTIFICATION_ID' ) && (int) MYFC_NOTIFICATION_ID !== 0 ) {
            if ( (int) $notification_id !== (int) MYFC_NOTIFICATION_ID ) {
                return $email;
            }
        }
        $selected = isset( $fields[ MYFC_SELECT_FIELD_ID ]['value'] )
            ? trim( $fields[ MYFC_SELECT_FIELD_ID ]['value'] )
            : '';
        $routes       = myfc_get_email_routes();
        $routed_email = MYFC_DEFAULT_NOTI_EMAIL;
        if ( isset( $routes[ $selected ] ) ) {
            $routed_email = $routes[ $selected ];
        }
        $email['address'] = array( $routed_email );
        return $email;
    }
    Plugin Support Amjad Ali

    (@amjadali688)

    Hi @nativebreed ,

    That’s great to hear. Thanks for coming back and sharing the solution!

    I’m glad you were able to track down the issue and get it working.

    Your final code and explanation will definitely be helpful for others who may run into the same situation.

    Thanks again for sharing your solution with the community!

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.