WebDesires

WordPress Contact Form 7 – Add email addresses to all forms

Need Some Help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request Support
There is a mobile optimized version of this page, view AMP Version.

Sometimes with Contact Form 7 you may have many forms, we have clients with 20+ forms on each of their sites, and sometimes you may with all your forms were sent to various staff members which change over time, the annoyance of Contact Form 7 is that to handle this you have to edit each and every form adding additional headers to change the CC: every time a new employee is needed on the forms and then again when they leave.

There is an easier way to do this though, you can add some code to your themes functions.php file, allowing you to add / edit / remove email addresses from all the forms at once, dynamically. You can even specify forms by their ID to single out one or two or more.

 

Add email to all Contact Form 7 forms

So firstly lets start with a basic catch all, adding an email to all Contact Form 7 forms using a CC header, please note that you can either append to the current “additional headers” or overwrite them completely as needed, just uncomment the line labelled “override additional headers”.

<?php
add_action('wpcf7_before_send_mail','dynamic_addcc');

function dynamic_addcc($WPCF7_ContactForm){


	$currentformInstance  = WPCF7_ContactForm::get_current();
	$contactformsubmition = WPCF7_Submission::get_instance();

	if ($contactformsubmition) {

		$data = $contactformsubmition->get_posted_data();

		if (empty($data))
			return;

		$mail = $currentformInstance->prop('mail');

		
		//$mail['additional_headers'] = ''; //override additional headers
		$mail['additional_headers'] .= "\r\nCc: person@mycompany.com";
		

		// Save the email body
		$currentformInstance->set_properties(array(
			"mail" => $mail
		));

		// return current cf7 instance
		return $currentformInstance;
	}
}
?>

Add email to selected Contact Form 7 forms

Ok so if you want to control different destinations for different forms, you just need to go find out the Contact Form ID and then setup IF statements like in the example below, again you can overwrite additional headers or append like above.

<?php
add_action('wpcf7_before_send_mail','dynamic_addcc');

function dynamic_addcc($WPCF7_ContactForm){

	// Check contact form id.
	$cfid = $WPCF7_ContactForm->id();
    

	$currentformInstance  = WPCF7_ContactForm::get_current();
	$contactformsubmition = WPCF7_Submission::get_instance();

	if ($contactformsubmition) {

		$data = $contactformsubmition->get_posted_data();

		if (empty($data))
			return;

		$mail = $currentformInstance->prop('mail');

		if (33 == $cfid || 43 == $cfid) {
			//$mail['additional_headers'] = ''; //override additional headers
			$mail['additional_headers'] .= "\r\nCc: person@mycompany.com";
		} else if (67 == $cfid || 22 == $cfid) {
			//$mail['additional_headers'] = ''; //override additional headers
			$mail['additional_headers'] .= "\r\nCc: person2@mycompany.com";
		} else {
			//$mail['additional_headers'] = ''; //override additional headers
			$mail['additional_headers'] .= "\r\nCc: person3@mycompany.com";
		}
		

		// Save the email body
		$currentformInstance->set_properties(array(
			"mail" => $mail
		));

		// return current cf7 instance
		return $currentformInstance;
	}
	
}
?>

Need Some Help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request Support

Author: Dean Williams

Professional PHP Web Developer with expertise in OpenCart Web Development, WordPress Web Development, Bespoke Systems - also a seasoned Linux Server Administrator.