Support » Plugin: Contact Form 7 » Issue with wpcf7_before_send_mail

  • Hi

    I am relatively new to wordpress and needed some help with the contact form 7 plugin.
    I tried to create a hook for wpcf7_before_send_mail by adding the following piece of code in the functins.php file of my active theme:

    function update_message_body($cf7){
    	if($cf7->id == 7){
    		$name = $cf7->posted_data["your-name"];
    		$cf7->mail['body'] = "Hello ".$name;
    	}
    }
    add_action("wpcf7_before_send_mail", "update_message_body");

    The purpose is to generate the message body dynamically based on user inputs and this code is just an initial test.
    After clicking the send button, however, the spin arrows keep rotating without giving the success message. Even though, the mail is delivered, the message body is not generated as required. Moreover, when this piece of code is removed the issue with the send arrows disappear indicating that there is some issue with my code.

    Could you tell me what I am doing wrong and how to troubleshoot this.

    I am using wordpress v3.9.2, contact form 7 v3.9.1.

    Thanks
    Varun

    https://wordpress.org/plugins/contact-form-7/

Viewing 15 replies - 1 through 15 (of 18 total)
  • same issue here. It would appear that the wpcf7_before_send_mail hook isn’t being triggered for some reason.

    I’ll post back if I find the answer.

    Thread Starter varu87

    (@varu87)

    Thanks daybo.

    any news on this? I’ve got the same issue.

    i’ve added the

    remove_all_filters (‘wpcf7_before_send_mail’);
    add_action(“wpcf7_before_send_mail”, “wpcf7_do_something”);

    but the hook is never called! 🙁

    I’m using WP Versione 3.9.2 and CF7 Versione 3.9.2

    Thanks

    Same issue here, WP Version 3.9.2 and CF7 Version 3.9.2

    Same issue, WP 3.9.2 CF7 3.9.3.

    The posted_data property is not a property of the WPCF7_ContactForm object, so you cannot get it like you did.

    I’m pretty sure it throws an error as like you said the spin keep rotating.
    Enable WP_DEBUG or watch your AJAX request in your browser dev tool.

    With this code, the hook is not called, I can’t see it in my browser dev tool (I only see the json for the succes message) :

    function wpcf7_modify_this(&$wpcf7_data) {
            $wpcf7_data->mail['body'].= "======================================================";
        }
        add_action("wpcf7_before_send_mail", "wpcf7_modify_this");

    >The posted_data property is not a property of the WPCF7_ContactForm object, so you cannot get it like you did.

    it certainly was though, because i just downgraded to 3.8 and it all works again…..(i have been using a similar filter as the op)

    PS: with 3.9 i got the following error

    Indirect modification of overloaded property WPCF7_ContactForm::$posted_data has no effect in /…/functions.php

    and skimming through the code posted_data is still in use it seems somehow

    (although now

    private $posted_data = array();

    whereas previously it was

    public $posted_data;

    )

    no idea if this could be the culprit, just passing on what i saw at a very very quick glance at stuff

    PPS: (and then i shut up) when using 3.9.3 with wpml i also got a javascript error (something like icl_vars not defined or something, can’t remember exactly)

    Use this code:

    add_action( 'wpcf7_before_send_mail', 'my_conversion' );
    function my_conversion( $cf7 )
    {
    $submission = WPCF7_Submission::get_instance();
    $data =& $submission->get_posted_data();
    $data['name'] = $data['first-name'];
    //Your codes go here
    }

    read here: http://techie4wordpress.tk/contact-form-7-version-3-9-hook-changes/

    @prolab

    hey

    thanks for the link and taking the time to actually post this here.
    apparently i seem to have missed that in the changelog …

    Thanks for the link prolab.
    For me collect data is not the problem. The proble is to updated the email. An idea ?

    add_action( 'wpcf7_before_send_mail', 'wishlist_email' );
        function wishlist_email( &$cf7 )
        {
            $submission = WPCF7_Submission::get_instance();
            $data =& $submission->get_posted_data();
            $tmp = '<table><tr><td>Ref1</td><td>Ref2</td><td>qty</td></tr>';
            if(!empty($data[wishlist])):
                foreach($data['wishlist'] as $key=>$item):
                    $tmp.= '<tr><td>'.$data['wishlist_fournisseur'][$key].'</td><td>'.$data['wishlist'][$key].'</td><td>'.$data['quantity'][$key].'</td></tr>';
                endforeach;
            endif;
            $tmp.= '</table>';
            $cf7->mail['body'].= $tmp;
        }

    @cvmh

    I’m having troubles with this too, but if you look at: http://contactform7.com/2014/07/02/contact-form-7-39-beta/

    You can see that they say to set the mail property with the set_properties method like so:

    $mail = $contact_form->prop( 'mail' );
    $mail['subject'] = "Well, hello, Dolly";
    $contact_form->set_properties( array( 'mail' => $mail ) );

    I would assume it would be the same for changing the body, but I haven’t been able to get it to work.

    I managed to get it to work using this method. What I had wrong was I was still passing the form to the callback by reference.

    In my case, I was adding to the existing body. Here is my final code that worked:

    add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
    
    function wpcf7_update_email_body($contact_form) {
      $submission = WPCF7_Submission::get_instance();
    
      if ( $submission ) {
        $mail = $contact_form->prop('mail');
        $mail['body'] .= '<p>An extra paragraph at the end!</p>';
        $contact_form->set_properties(array('mail' => $mail));
      }
    }

    I hope this helps.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Issue with wpcf7_before_send_mail’ is closed to new replies.