FAQs

How can I send an email to different addresses?

It's often useful to be able to send emails to different addresses depending on the selections the user has made in the form. For example., a Customer Service form might be sent to different departments depending on the type of question that is being asked.
It is tempting to try to do this by putting the emails in the Form HTML. This will work but is a security risk as any email in your HTML may be scraped and added to a spammers list.
Here is how to select the correct email address after the form is submitted.

CFv6 version

Supposing your dropdown field or radio button is named "user_email", go under "Events" > "Logic" and drag a "Switch" action to the "submit" event, make sure the new switch action is BEFORE (above) the "Email" action.

In the "Data provider" field of the "Switch" action write: {data:user_email}

In the "Values setup" enter multiple lines, each of them in this format: dropdown_value:email_address

em1:first@domain.com
em2:second@domain.com
em3:third@domain.com

Now set the "Switch" action "Name" to "email_address" for example, in your "Email" action "Recipients" just write {var:email_address} to capture the result of the switch action which is an email address.

CFV5 version

a) I used a Select Box element (or a Radio Button element) in the Designer tab with the name & id set to recipients and the options:
em1=This address
em2=That address
em3=The other address
b) In the OnSubmit event I have a Custom Code action with this code:
<?php
// set a default recipient
$recipient = 'em1';
if ( isset($form->data['recipients']) && $form->data['recipients'] ) {
  $recipient = $form->data['recipients'];
}
$emails = array (
  'em1' => 'bob@example.com',
  'em2' => 'info@example.net',
  'em3' => 'admin@example.com' );
$form->data['email_to_use'] = $emails[$recipient];
?>

OR if you want the user to be able to select more than one address. In this case my form has a select dropdown or checkbox group named recipients[].

// set a default recipient
$recipients = array( 'em1' );
if ( !empty( $form->data['recipients'] ) ) {
  $recipients = $form->data['recipients'];
}
$emails = array (
  'em1' => 'bob@example.com',
  'em2' => 'info@example.net',
  'em3' => 'admin@example.com' 
);
$emails_to_use = array();
foreach ( $recipients as $v ) {
  $emails_to_use[] = $emails[$v];
}
$emails_to_use = implode(',',  $emails_to_use);
$form->data['emails_to_use'] = $emails_to_use;
?>
OR in the same case where some options require sending to more than one address. Note that the list of addresses now uses arrays of addresses.
<?php
// set a default recipient
$recipients = array( 'em1' );
if ( isset($form->data['recipients']) && $form->data['recipients'] ) {
  $recipients = $form->data['recipients'];
}
$emails = array (
  'em1' => array('bob@example.com', 'john@example.com'),
  'em2' => array('info@example.net'),
  'em3' => array('admin@example.com', 'admin@example.org') 
);
$emails_to_use = array();
foreach ( $recipients as $v ) {
  $emails_to_use = array_merge($emails[$v], $emails_to_use);
}
$emails_to_use = implode(',',  $emails_to_use);
$form->data['emails_to_use'] = $emails_to_use;
?>
OR to send one or more addresses entered in the form data. In this case there are two email inputs, the second is optional but if it is used then the email should be sent to both addresses:
<?php
if ( !empty($form->data['email2']) ) {
  $mailto = array($form->data['email1'], $form->data['email2']);
  $mailto = implode(',', $mailto);
} else {
  $mailto = $form->data['email1'];
}
$form->data['email_to_use'] = $mailto;
?>

c) Also in the OnSubmit event after the Custom Code action I have an Email action with the Dynamic To box on the Advanced tab set to email_to_use or emails_to_use The email action is enabled and has standard Static values for Subject, From Name and From Email.

 

CFV4 version

a) I used a Select Box element (or a Radio Button element) in the Form Preview with the name & id set to recipients and the options:
em1=This address
em2=That address
em3=The other address 
b) In the OnSubmit event I have a Custom Code action with Mode = Controller and this code:
<?php
$recipient = JRequest::getString('recipients', 'em1', 'post');
$emails = array (
  'em1' => 'bob@example.com',
  'em2' => 'info@example.net',
  'em3' => 'admin@example.com' );
$form->data['email_to_use'] = $emails[$recipient];
?>
c) Also in the OnSubmit event I have an Email action with the Dynamic To set to email_to_use It is enabled and has standard Static values for Subject, From Name and From Email.

For ChronoForms v3 and earlier

First include the following in your Form HTML:

. . .
Select the recipient you wish to contact:
<select name="recipients">
  <option value="em1">Name 1
  <option value="em2">Name 2
  <option value="em3">Name 3
  . . . 
</select>
. . . 

Then enter this code in the 'On Submit code - before sending email' field:

For ChronoForms 3.1 and 3.2

<?php
$emails_2 = array( 
  'em1'=>'sample1@email.com', 
  'em2'=>'sample2@email.com', 
  'em3'=>'name_3@example.com', 
  . . . 
); 
$MyForm =& CFChronoForm::getInstance('form_name_here'); 
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id); 
$MyFormEmails->setEmailData(1, 'to', $emails_2[$_POST['recipients']]); 
?>

For ChronoForms v3.0

<?php
$emails_2 = array('em1'=>'sample1@email.com', 'em2'=>'sample2@email.com', 'em3'=>'name_3@example.com', . . .);
$emails[0]->to .= ','.$emails_2[$_POST['recipients']];
?>

For ChronoForms 2.5.x and earlier

<?php
$emails = array(
  'em1'=>'name_1@example.com', 
  'em2'=>'name_2@example.com', 
  'em3'=>'name_3@example.com', 
  . . .
);
$rows[0]->extraemail = $emails[$_POST['recipients']];
?>

Thanks to deafjoomla.com for the original version of this FAQ