Search code examples
wordpresscontact-form-7newsletter

How to get contact email from submitted data in Contact Form 7?


I would like to subscribe the user to my other newsletter system using Contact Form 7. I tried to get the recipient email from the submitted form with the below code, but it returns sender (admin) email.

add_action('wpcf7_before_send_mail', function ($contact_form) {
    $mailProp = $contact_form->get_properties('mail');
    subscribe_to_another_newsletter($mailProp['mail']['recipient']);
});

How can I get the contact's data?


Solution

  • You want to grab your submission and then use the form field you used for email to do what you want with it.

    add_action( 'wpcf7_before_send_mail', array($this, 'cf7_process_form'));
    function my_cf7_process_form(){
        // This calls the static get the cf7 form data
        $submission = WPCF7_Submission::get_instance();
           if ( $submission ) {
            // $posted_data = array with all form fields
            $posted_data = $submission->get_posted_data();    
        }
        // if [your-email] is the form tag
        $email = $posted_data['your-email'];
        subscribe_to_another_newsletter($email);
    }