cf7-repeatable-fields icon indicating copy to clipboard operation
cf7-repeatable-fields copied to clipboard

Allow Repeatable email fields to be used in the TO and Mail Headers for notifications

Open briandesp opened this issue 6 years ago • 9 comments

briandesp avatar Aug 11 '18 22:08 briandesp

Hello @felipeelia Any update regarding this problem.

Thanks

YuvrajKhavad avatar Apr 04 '19 13:04 YuvrajKhavad

I have an [email] field in my repeater and I needed to send a confirmation to each of those emails from Mail 2. If you have a similar situation, you can add the following code to your functions.php file Be sure to review the comments to update the code where necessary.

// make sure plugin is active on site
if ( is_plugin_active( 'cf7-repeatable-fields/cf7-repeatable-fields.php' ) ) {
	
	/* Allows multiple email fields from the repeatable email field in form #78223 in Mail 2
	*/
	function fx_wpcf7_repeatable_recipient($mail2, $form) {
		// BE SURE TO UPDATE BELOW WITH YOUR OWN FORM ID
		if ( $form->id != 78223 ) return $mail2;
		$to = array();
		// I added some JS to prevent the user from adding more than 4 emails, but you can add more if you need
		if( isset( $_POST['email__1'] ) )
			$to[] = $_POST['email__1'];
		if( isset( $_POST['email__2'] ) )
			$to[] = $_POST['email__2'];
		if( isset( $_POST['email__3'] ) )
			$to[] = $_POST['email__3'];
		if( isset( $_POST['email__4'] ) )
			$to[] = $_POST['email__4'];

		$to = implode( ',', $to);
		$mail2['mail_2']['recipient'] = $to;
		
		return $mail2;
	}
	add_action( 'wpcf7_additional_mail', 'fx_wpcf7_repeatable_recipient', 99, 2);
}

chloemccarty avatar Dec 09 '19 20:12 chloemccarty

Thanks for sharing your solution @chloemccarty !

felipeelia avatar Dec 10 '19 13:12 felipeelia

This is great! @chloemccarty can you share the JS to prevent the user from adding more than X number of repeater fields as well?

keeslina avatar Feb 11 '20 13:02 keeslina

Recently I had a similar scenario as @chloemccarty, where I need to send emails through Mail to multiple recipients. Also, I use repeatable [emails] field. Following the provided solution for Mail 2 and after modifying code to fit on the Mail form, I added the following code in the functions.php.

// make sure plugin is active on site
if ( is_plugin_active( 'cf7-repeatable-fields/cf7-repeatable-fields.php' ) ) {	
	// define the wpcf7_mail_components callback 
	function filter_wpcf7_mail_components( $components, $form, $instance ) { 
		// make filter magic happen here... 
                // BE SURE TO UPDATE BELOW WITH YOUR OWN FORM ID
		if ( $form->id != [FormID]) return $components;
		$to = array();		
		$i = 1;
		while( isset( $_POST['friend-email__'.$i] ) ) {
    		        if( isset( $_POST['friend-email__'.$i] ) ) {
				$to[] = $_POST['friend-email__'.$i];
				$i++;
			}
		}
		$to = implode(',', $to);
		$components['recipient'] = $to;
		return $components; 
	}; 

	// add the filter 
	add_filter( 'wpcf7_mail_components', 'filter_wpcf7_mail_components', 10, 3 ); 
}

HristinaJ avatar Feb 11 '20 22:02 HristinaJ

@HristinaJ @chloemccarty Any idea how to get this working in the additional headers (Bcc) rather than just the To field?

n-mitchell avatar Jun 03 '20 09:06 n-mitchell

@n-mitchell I think it can be achieved using $components['additional-headers'] instead of $components['recipient'], but haven't tested it.

$components['additional_headers'] = "Bcc: $bcclist";

HristinaJ avatar Jun 04 '20 14:06 HristinaJ

@HristinaJ Thanks, got it working with this:

$components['additional_headers'] = "Bcc: $bcclist";

n-mitchell avatar Jun 05 '20 15:06 n-mitchell

Are these solutions mentioned above still applicable to the latest version? How are we able to send an email confirmation to repeated emails?

cscupcake avatar Sep 04 '23 07:09 cscupcake