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.