Feature Request: Ability to Send Emails to Repeater Field Values After Form Submission
hafizaqibshafique opened this issue · 1 comments
I'm using JetFormBuilder to create a form that includes a repeater field where users can enter multiple email addresses. I would like to send an email to each of the email addresses listed in the repeater field after the form is submitted.
Use Case:
The repeater field (emails_array) contains multiple email addresses, and after the form is submitted, I need JetFormBuilder to automatically send an email to each address. I tried using a custom action hook, but I haven't been able to get it to work reliably.
Example Code:
Here’s an example of what I’ve attempted so far:
`add_action( 'jet-form-builder/form-handler/action/after', 'send_emails_to_repeater_field', 10, 2 );
function send_emails_to_repeater_field( $handler, $form_id ) {
$participants = isset($handler->form_data['emails_array']) ? $handler->form_data['emails_array'] : [];
if ( ! empty( $participants ) && is_array( $participants ) ) {
foreach ( $participants as $participant ) {
$email = isset($participant['emails']) ? $participant['emails'] : '';
if ( ! empty( $email ) ) {
wp_mail( $email, 'Subject Here', 'Message Here' );
}
}
}
}`
Despite this, the emails are not being sent. It would be incredibly helpful to have a native or more reliable method to send emails to addresses listed in a repeater field.
Request:
Can you provide guidance on how to properly implement this functionality?
Alternatively, could this feature be added as a built-in option for the email notification settings within JetFormBuilder?
Environment:
JetFormBuilder
WordPress version:
PHP version:
Additional Context:
Any additional tips or insights into working with repeater fields in JetFormBuilder would be greatly appreciated.
Thank you!
Unfortunately, we do not plan to introduce such functionality into our plugin. Now our team is working on more priority tasks. But in the future we do not exclude the possibility of adding this functionality to the core of our plugin.
For now, you can use custom code to solve the issue.
First, add a hidden field with the emails_from_repeater name (or use a custom name, but you will need to update it in the code below) https://i.imgur.com/OaFD45g.png.
Second, add a repeater field with a text field inside.
Third, add the call a hook action before the send mail action. In the action, add the set-emails hook name https://i.imgur.com/t6gx7do.png.
Then, in the send email action, select your hidden field where the emails will be stored https://i.imgur.com/PCukbvx.png.
Finally, add the code in the code snippet
add_action( 'jet-form-builder/custom-action/set-emails', function( $request, $action_handler ) {
$repeater_field_name = 'emails'; // repeater field name
$repeater_subfield_name = 'enter_email'; // text repeater sub-field
$hidden_field = 'emails_from_repeater'; // hidden field where store all emails from text repeaters field
$emails_list = [];
if ( ! empty( $request[ $repeater_field_name ] ) ) {
foreach( $request[ $repeater_field_name ] as $repeater_item ) {
if ( ! empty( $repeater_item[ $repeater_subfield_name] ) ) {
$emails_list[] = $repeater_item[ $repeater_subfield_name];
}
}
}
$emails_list = implode( ', ', $emails_list );
$action_handler->response_data[ $hidden_field ] = $emails_list;
jet_fb_context()->update_request( $emails_list, $hidden_field );
}, 10, 2 );
result: https://i.imgur.com/96zMlRI.png