Contact Form 7 - Dynamic Recipient Email Address

I am trying to get the recipient’s email dynamically from user fields and use line replacement to change the email of the contact’s recipient 7. The contact form is being sent, but it doesn’t seem to change the email address of the recipient since I do not receive the email.

<?php
function wpcf7_dynamic_email_field( $args ) {

    $dynamic_email = '';
    $submission = WPCF7_Submission::get_instance();
    $unit_tag = $submission->get_meta( 'wpcf7-f3936-p3933-o1' );

    // get the post ID from the unit tag
    if ( $unit_tag && preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $unit_tag, $matches ) ) {
        $post_id = absint( $matches[2] );
        $dynamic_email = get_post_meta( $post_id, 'email', true );
    }
    if ( $dynamic_email ) {
        $args['recipient'] = str_replace('emailtoreplace@email.com', $dynamic_email, $args['recipient']);
    }

    return $args;
}

add_filter( 'wpcf7_mail_components', 'wpcf7_dynamic_email_field' );
?>

I am running CF7 4.5.1 and PHP 5.3. Am I missing something here?

+4
source share

Source: https://habr.com/ru/post/1657246/


All Articles