Contact Form 7: send copy to sender with checkbox in 3.9 Update

Contact Form 7 (CF7) is a popular wordpress plug-in to create powerful contact forms for wordpress websites. To those familiar with the PHP language, it opens a way to customize contact forms to fit their special needs.

I was ordered to make a contact form for a wordpress site with a checkbox which controls whether the sender gets a copy of his sent mail or not. Because I used to be very happy with CF7 in past projects, I decided to use it again, and since CF7 doesn’t provide an optional „send e-mail copy“ checkbox out of the box, I did some research and found a lot of forum discussions and documentation like this one.

I started on this topic on July 17th with CF7 version 3.9, and unfortunately it took me about 4 hours to find out that all those past forum posts with code snippets are useless since July 14th when Contact Form 7 version 3.9 was published! I have to say it bold and loud again:

Most of your customized code (if not all) for wordpress Contact Form 7 prior to version 3.9 won’t work anymore in version 3.9+.

According to this article by Contact Form 7 publisher Takayuki Miyoshi, the wpcf7_before_send_mail function works in a different way now.

Your old code

This is how your „send me a copy“ checkbox function will probably look like in your code:

You probably have a shortcode like this in your CF7 contact form:

[checkbox contact_sendmail "Send me a copy of this message"]

And you will have something like this in your active theme’s function.php:

add_filter( 'wpcf7_additional_mail', 'my_wpcf7_check_for_mail2', 10, 2 );
function my_wpcf7_check_for_mail2 ( $additional_mail, $cf7 ){
    if ( "Send me a copy of this message" != $cf7->posted_data['send_c'] )
        $additional_mail = array();
    return $additional_mail;
}

This won’t work anymore.

My new code

In my project, I decided to use the Mail (2) function of CF7: instead of setting up the sender’s address as CC, I send a second mail with differing content. My workaround just works with Mail (2) and not with CC/BCC, so you have to do it the same way or change the code to your desires.

My code does the following: we leave the recipient field in Mail (2) blank, and it will be filled with the sender’s e-mail address if the checkbox at the frontend is checked. So either there will be a second mail send or not.

Let’s get to work! Do the following:

1. Open the CF7 contact form which you want to edit in wordpress Admin area. You set up the [checkbox] snippet which produces the checkbox in the contact form on your website:

[checkbox contact_sendmail "Send me a copy of this message"]

2. Activate Mail (2) (the mail copy which will be sent to the sender), fill it out to fit your needs, leave the To: field blank! Don’t put [your-mail] etc. in it.

3. In your active theme’s function.php, delete your old wpcf7_before_send_mail function (if you have one). Add the following code:

function check_mail_send_contactform($cf7) {
    //get CF7's mail and posted_data objects
    $submission = WPCF7_Submission::get_instance();
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }
    $mail = $cf7->prop( 'mail' );

    if($posted_data['contact_sendmail'][0]) { //if Checkbox checked
        $mail2 = $cf7->prop( 'mail_2' ); //get CF7's mail_2 object
        //now set sender's address to mail2's recipient
        $mail2['recipient'] = $mail['sender'];
        $cf7->set_properties( array( 'mail_2' => $mail2 ) );
    }
    return $cf7;
}
add_action('wpcf7_before_send_mail','check_mail_send_contactform');

What does it do?

Firstly, we load CF7’s objects in arrays to make them accessible to us (if you are interested in getting deeper in this topic: Xavi Esteve wrote an excellent post about CF7’s objects). Afterwards we look if the checkbox was checked (we don’t need to know about the content of the checkbox, just if there is anything) and if yes, we set the sender’s address to the recipient of the second mail.

20 Gedanken zu „Contact Form 7: send copy to sender with checkbox in 3.9 Update

  1. Hi,
    danke für den Code!
    Leider funktioniert das auch nicht mehr in der Version CF7 4.1. Gibt es diesbezüglich eventuell ein Update oder mache ich komplett was falsch?

    Alles wie oben beschrieben eingefügt. Klappt es bei euch?

    bg
    Jan

  2. Hi, thanks for this post. I’m using Contact Form 4.1. I try the code but is not working.
    this is my form tag:
    text* your-name placeholder“NOM *“]

    [email* your-email placeholder“EMAIL *“]

    [text your-subject placeholder“SUJET *“]

    [textarea your-message placeholder“MESSAGE *“]

    [checkbox contact_sendmail „Send me e copy“]

    [submit „Envoyer“]

    First. I thought I should change the $mail[’sender‘] to $mail[‚your-email‘]. But this change nothing. No mails is sent to the user when the checkbox is checked

  3. Hello, I have found another work around based on this code also.

    So, use exact same steps as above BUT put the email address in the Mail_2 ( for ex: [your-email] ), then deactivate the Mail_2.

    And the code is this:

    function check_mail_send_contactform($cf7) {
    //get CF7’s mail and posted_data objects
    $submission = WPCF7_Submission::get_instance();
    if ( $submission ) {
    $posted_data = $submission->get_posted_data();
    }
    $mail = $cf7->prop( ‚mail‘ );

    if($posted_data[‚contact_sendmail‘][0]) { //if Checkbox checked
    $mail2 = $cf7->prop( ‚mail_2‘ ); //get CF7’s mail_2 object
    //now activate mail_2, the value 0 means not active
    $mail2[‚active‘] = 1;
    $cf7->set_properties( array( ‚mail_2‘ => $mail2 ) );
    }
    return $cf7;
    }
    add_action(‚wpcf7_before_send_mail‘,’check_mail_send_contactform‘);

    That will be all. It works for me very well.

    Hope to be helpful for others too.

    Happy coding ! 🙂

  4. please be careful when copying the code.
    especially the ‚ will turn into ` or something similar when pasting into the functions.php
    last post worked in version 4.1.2 after correcting the copy errors

  5. Hi ,

    I am using 4.5.4 wordpress version and the plugin is 4.5(latest version)
    I tried the above code and also i tried the “ andrei nagacevschi“ steps. But this is not working(the sender’s mail address will not receive the copy). I am trying to fix this problem for the past 1 week. Please any one help me to fix this issue. Is there any code changes for the latest version now?? Please reply me if anyone have the solution. I am eagerly waiting for the reply!!

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.