cf7-repeatable-fields
cf7-repeatable-fields copied to clipboard
Allow Repeatable email fields to be used in the TO and Mail Headers for notifications
Hello @felipeelia Any update regarding this problem.
Thanks
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);
}
Thanks for sharing your solution @chloemccarty !
This is great! @chloemccarty can you share the JS to prevent the user from adding more than X number of repeater fields as well?
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 @chloemccarty Any idea how to get this working in the additional headers (Bcc) rather than just the To field?
@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 Thanks, got it working with this:
$components['additional_headers'] = "Bcc: $bcclist";
Are these solutions mentioned above still applicable to the latest version? How are we able to send an email confirmation to repeated emails?