Developer Support »

Select form recipient

This applies to both scripts, FormToEmail and FormToEmail-Pro

With a standard installation the script will email the data from your form to one or more email addresses that are hard-coded into the script. You might want to give your visitor a choice of recipients to select from to send the form to. Typically you would offer these choices in a drop-down list with the visitor selecting one of them. The email containing the form data will be sent to the selected recipient.

You can offer this facility to your visitors by putting some HTML code in your form and adding some PHP code to the script.

Form code. To make a drop-down select in your form, copy the HTML code below and paste it into your form:

<select name="recipient">
<option value="">Choose a recipient</option>
<option value="recipient_1">Sales</option>
<option value="recipient_2">Support</option>
<option value="recipient_3">Customer service</option>
</select>

For the script to handle this input you need to add some PHP code to it. Open up the script in a text editor and locate this line, near the bottom:

mail($my_email,$subject,$message,$headers);

Above it, paste in this code:

$recipients = array(

'recipient_1' => '[email protected]',
'recipient_2' => '[email protected]',
'recipient_3' => '[email protected]'

);

$my_email = $recipients[$_REQUEST['recipient']];

Note that the keys in the $recipients array correspond to the values in your form code. You can have as many options as you wish in the select list, just make sure that you have the corresponding email addresses in the $recipients array and that you follow the same naming convention above. Note, the last email address in the $recipients array does not have a comma after it.

That's all. Save the HTML file (webpage) that your form is on. Save the script then upload both files to your webspace.

When using this method you do not need to enter your email address(es) in the script in the standard way. If you do, it will be safely ignored and the email from the form will go to the selected recipient.

You will get the selected recipient showing up in the body of the email like "Recipient: recipient_1". If using the free script you will have to put up with this (for all it is). If you are using the Pro script you can use an email template so you can omit the recipient field from the body of the email. See the script instructions for information about using a template. Due to the positioning of the above code in the script, you cannot use the $ignore_fields facility in the script to remove the recipient field from the email.

Selecting recipients by checkbox

This demonstrates another way to send the email from the script to different recipients depending on visitor selection. This is based on a "real life" example from a customer who wanted the email to go only to certain recipients depending on which checkboxes were checked on the form. This was on a wedding website. The visitor could enter their contact information in the form, then check checkboxes to indicate which suppliers they would like to hear from. The script would then send the email with the visitor's contact information only to those suppliers who were in the selected categories.

For each wedding category there were several suppliers. The requirement was to make sure that every supplier in the chosen category got sent the email, regardless of how many suppliers were in each category and regardless of how many categories were selected. The code below, shows you how to achieve this. It is shorter than the code in the "real life" instance, but it shows you all you need to know.

For this example, the suppliers are caterers, photographers, bakers and florists.

Put this code on your form page, to make the checkboxes:

<input type="checkbox" name="suppliers[]" value="caterers"> Caterers
<br>
<input type="checkbox" name="suppliers[]" value="photographers"> Photographers
<br>
<input type="checkbox" name="suppliers[]" value="bakers"> Bakers
<br>
<input type="checkbox" name="suppliers[]" value="florists"> Florists

Open up your script (FormToEmail.php or formtoemailpro.php) and locate this line (same as the example above):

mail($my_email,$subject,$message,$headers);

Above it, paste in this code:

if(isset($_REQUEST['suppliers']) && count($_REQUEST['suppliers']))
{

$recipient_email_addresses = array
(

"caterers" => array('[email protected]','[email protected]'),
"photographers" => array('[email protected]'),
"bakers" => array('[email protected]','[email protected]','[email protected]'),
"florists" => array('[email protected]','[email protected]','[email protected]','[email protected]')

);

foreach($_REQUEST['suppliers'] as $value){foreach($recipient_email_addresses[$value] as $value2){mail($value2,$subject,$message,$headers);}}

}

Save your form page and edited script and upload them to your webspace.

Some points

The checkboxes all have the same name, "suppliers[]". The name is followed by square brackets (a left bracket and a right bracket). This puts the checkbox values into an array, which is needed for the code to handle them. Replace "suppliers" with the name of your checkboxes in the form code and the script code above.

Put the recipient email addresses in the $recipient_email_addresses array. You can see the categories in that array. Each category has a corresponding array containing the email addresses for that category. Put the email addresses in quotes and separate them with a comma. There is no limit to the number of email addresses that you can put in each array. Note that the last element of the $recipient_email_addresses array ("florists"...) does not have a comma at the end of the line.

Name the keys of the $recipient_email_addresses array ("caterers", "photographers", "bakers" and "florists") exactly as they are named in the checkbox values.

Using this method, you can still enter an email address (or addresses) in the $my_email variable in the script, which means that an email from the script will also go to that address for every form submitted.

Send to multiple recipients from a single form input

This can be handy if you have more than one form and are using one copy of the script to process the forms. You can use (for example) a hidden input on a form to pre-select the recipients for the output email from the script. This example shows how to send to multiple recipients.

Put this input on your form:

<input type="hidden" name="recipient" value="recipient_1,recipient_2">

In the script, replace the mail line:

mail($my_email,$subject,$message,$headers);

...with this code:

$recipients = array(

'recipient_1' => '[email protected]',
'recipient_2' => '[email protected]',
'recipient_3' => '[email protected]'

);

$exploded_recipients = explode(",",$_REQUEST['recipient']);

foreach($exploded_recipients as $value)
{

$my_email = $recipients[$value];

mail($my_email,$subject,$message,$headers);

}

You can replace "recipient_1", "recipient_2" etc on your form and script code, with "01", "02" etc (or anything you like, so long as the script code matches the form code). In the above example, [email protected] and [email protected] will get an email.