Contact Form 7 的条件自动回复

Conditional Autoresponder for Contact Form 7

正在尝试实现联系表 7 的有条件自动回复,具体取决于输入字段中的内容。此线程 (Conditional auto responder is Contact Form 7) 提出了一个解决方案,但通过“片段”插件实现代码似乎不起作用 - 没有发送邮件响应。

如果可以,请指教如何用cf7实现下面的代码。谢谢,

#hook in to wpcf7_mail_sent - this will happen after form is submitted

add_action( 'wpcf7_mail_sent', 'contact_form_autoresponders' ); 

#our autoresponders function

function contact_form_autoresponders( $contact_form ) {

   if( $contact_form->id==1234 ){ #your contact form ID - you can find this in contact form 7 settings

        #retrieve the details of the form/post
        $submission = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();                          

        #set autoresponders based on dropdown choice            
        switch( $posted_data['location'] ){ #your dropdown menu field name
            case 'California':
            $msg="California email body goes here";
            break;

            case 'Texas':
            $msg="Texas email body goes here";
            break;

        }

        #mail it to them
        mail( $posted_data['your-email'], 'Thanks for your enquiry', $msg );
    }
}

下拉列表中存储的数据默认为数组。既然如此,你就很接近了。但是,您还应该使用 wp_mail 而不是 mail

add_action( 'wpcf7_mail_sent', 'contact_form_autoresponders' );

function contact_form_autoresponders( $contact_form ) {
    // The contact form ID.
    if ( 1234 === $contact_form->id ) {
        $submission  = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();
        // Dropdowns are stored as arrays.
        if ( isset( $posted_data['location'] ) ) {
            switch ( $posted_data['location'][0] ) {
                case 'California':
                    $msg = 'California email body goes here';
                    break;
                case 'Texas':
                    $msg = 'Texas email body goes here';
                    break;
                default:
                    $msg = 'Unfortunately, that location is not available';
            }
            // mail it to them using wp_mail.
            wp_mail( $posted_data['my-email'], 'Thanks for your enquiry', $msg );
        }
    }
}