How to send an Email dynamically


"There should be an additional check box in the form where the sender can request an e-mail notification." A user asked how to do this today, the solution is easy, as long as you know how to do it, here is how.

All actions have an enabled parameter to control their status when they are executed, we will have to to enable this parameter (set it to 1) if the checkbox is checked, we will do this using a "Custom Code" action.

First, make sure that your email action is configured correctly but is "Disabled".

Please drag a "Custom Code" action BEFORE the "Email" action and insert the code below:

if ( !empty($form->data['input_checkbox_1']) && ($form->data['input_checkbox_1'] == '1') ) {
  // $form->set("email_77", "enabled", 1); // this line will work only on CFv4
  // $form->actions_config[77]["enabled"] = 1; // this line will work only on CFv5

This code above will change the 'enabled' parameter in the Email action with ID = 77 from 0 to 1, which will enable the email action in the run time before its processed.

Some important notes:

  • Please remember that the action id number is written in red beside the action name in the form wizard.
  • The parameters names can be found in the source .php files of the actions.
  • The same solution above can be used to change any action's parameter dynamically, as long as you change the action id and parameter name accordingly.

In CFv5 there is an Event Switcher action that may be easier to use than this method. Please check this FAQ

Bob's custom Email [GH] action includes a conditional option that will let you do this without PHP for CFv4.