Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Send copy, if radio checked – Based on http://www.motomax.de/wordpress/contact-form-7-send-copy-to-sender-with-checkbox-in-3-9-update/
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(isset($posted_data['checkbox'][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'] = $posted_data['your-email'];
$cf7->set_properties( array( 'mail_2' => $mail2 ) );
}
return $cf7;
}
add_action('wpcf7_before_send_mail','check_mail_send_contactform');
@jose-serrao
Copy link

jose-serrao commented Oct 18, 2017

Hi, first of all thank you for your snippet. I need to point that to make it work I needed to change this "$posted_data['checkbox'][0]" to "$posted_data['contact_sendmail'][0]"

all the best

@mathiasmadsen
Copy link
Author

mathiasmadsen commented Nov 27, 2020

FYI: This still works with latest Contact Form 7 implementation (5.3) and the latest Wordpress installation (5.5.3).

Note that 'checkbox' reflects the name in the contact form (the second string), e.g.
[checkbox checkbox use_label_element "Send a copy of my email e-mail"]

I was just lazy and decided to call the checkbox field 'checkbox' whereas I can see jose-serrao used the name 'contact_sendmail'.

@johnzuh
Copy link

johnzuh commented Jan 11, 2021

This did it for me too if(isset($posted_data['contact_sendmail'][0]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment