How to change data before sending in Contact form 7?

I have contact form on my website. And I need to change the value of one field before sending mail. For example name. I try like this:

function contactform7_before_send_mail( $cf7 ) {
    $cf7->posted_data['your_name'] = 'John Doe';
}

add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

But in the email comes the value that is specified in the form.

Related posts

2 comments

  1. Recently faced the same problem.

    There is a field in this form called “[s2-name]” for example. When a visitor submit out a form, I want to get this field, then change and send. After some information searching, I wrote this code:

        add_action( 'wpcf7_before_send_mail', 'wpcf7_do_something_else_with_the_data', 90, 1 );
        
        function wpcf7_do_something_else_with_the_data( $WPCF7_ContactForm ){
        
            // Submission object, that generated when the user click the submit button.
            $submission = WPCF7_Submission :: get_instance();
        
            if ( $submission ){
                $posted_data = $submission->get_posted_data();      
                if ( empty( $posted_data ) ){ return; }
                
                // Got name data
                $name_data = $posted_data['s2-name'];
        
                // Do my code with this name
                $changed_name = 'something';
        
                // Got e-mail text
                $mail = $WPCF7_ContactForm->prop( 'mail' );
        
                // Replace "[s2-name]" field inside e-mail text
                $new_mail = str_replace( '[s2-name]', $changed_name, $mail );
        
                // Set
                $WPCF7_ContactForm->set_properties( array( 'mail' => $new_mail ) );
                
                return $WPCF7_ContactForm;
            }
        }
    

    Tested with: WP 5.4.2 and Contact Form 7 Version 5.2

  2. Try this:

    post.php

        $_POST["your_name"] = "John Doe");
        do_shortcode("[cfdb-save-form-post]");
    

    form.html

    <form class="form-contact" action="post.php" method="post">
      <input type="text" name="your_name" />
    </form>
    

Comments are closed.