Switcher Function in Chronoforms 8

How to send emails to different recipients based on form field selection in ChronoForms 8.

Overview

The issue occurs because the dedicated Switch feature from earlier versions is not available, and initial attempts to set a variable via PHP code for the email recipient fail.
Create a PHP action that returns the email address based on the form field value, place this action before the email action in the form event, and use the PHP action's variable name as the recipient in the email settings.

Answered
ChronoForms v8
sp spytech 27 May, 2024

Hi, where is the Switch feature in Chronoforms 8? I need to email to a specific user based on a field on the contact form. I used the switch feature before in v.6

Thanks.

rb rbock 03 Jun, 2024
Answer
1 Likes

Use a PHP action based on the value of your field. if... {return email1@mail.de;"} etc. You name the PHP action switch_mail, for example. 

In the mail action, you use {var:switch_mail} as the recipient. 

Or you include the mail action in the event of the PHP action. if... {return email1;"} ... if... {return email2;"} etc. , add an event email1 and here the mail1 action, add an event email2 and here the mail2 action etc...

I think that's how it should work.

sp spytech 12 Sep, 2024

Hi, I tried this but with no luck.

Can you be a little more specific. 

I tried it in both and then added the variable in the email but just got an error saying "Invalid address: (to):"

I added this PHP code to a PHP Action on the 'Submit' tab :

if (isset($form->data['contact_type'])) { $contact_type = $form->data['contact_type']; // Switch based on contact_type if ($contact_type == 'Maintenance') { $form->data['email_add'] = 'maintenance@studentbeehive.co.uk'; } elseif ($contact_type == 'Contact') { $form->data['email_add'] = 'info@studentbeehive.co.uk'; } else { $form->data['email_add'] = 'default@studentbeehive.co.uk'; // Fallback in case no match } // Return the email for backward compatibility or debugging (optional) return $form->data['email_add']; }

Then added the variable in the email: {var:email_add}

This is my process

  • First, the form captures the user's selection (either 'Maintenance' or 'Contact').
  • The PHP Action (switch_mail) sets the appropriate email address and stores it in the email_add variable.
  • The Email Action then uses {var:email_add} as the recipient email.

But does not work.

Max_admin Max_admin 14 Sep, 2024
1 Likes

if you do a PHP code switching like this:

$email = "1@domain.com";
if($this->data("field") == "2"){
$email = "2@domain.com";
}
return $email;

and your PHP action name is php_5 then you can use {var:php_5} as the recipient, but your PHP action should be before the email action

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.