Send Contact form to chosen department email
Wednesday, September 5th, 2007I recently had the following question from a FCF user:
QUESTION
I would love to use your form in a website i am working on at the moment. But i want to be able to let the user select which department to send the email to rather than a single fixed email address. Is this possible?
ANSWER
Yes it is possible with a few modification. This modification is a little harder to follow than any previous one, but I’m sure you will get on just fine. Ready? good, now follow the steps below.
Step One: Setting up the Configuration
Open config.php in your favorite text/script editor.
Step Two:
Go-to line number 31 (or there abouts), look for the following line of code:
$email_it_to = "enter_your_email_address_in_here";
Step Three:
We need to update this line to support multiple email addresses, change the above line to:
$email_it_to = array("department1@yoursite.com", "department2@yoursite.com", "department3@yoursite.com");
The above now supports 3 email options (change the email address as you like) - you can add as many as you like (just separate the by a comma and have the email address enclosed in double quotes as above). We now have an array of email addresses.
Step Four:
Save config.php, we are finished with that file.
Step Five: Updating your Form
Open form.php in your favorite text/script editor.
Step Six:
Go-to line number 47 (or there abouts), look for the following lines of code:
<div class="r">
<label for="fullname" class="req">Full Name: <em>*</em></label>
Step Seven:
Directly above line 47, enter the following code:
<div class="r">
<label for="department" class="req">Department: <em>*</em></label>
<span class="f">
<select name="department" id="department">
<option value="">Please Select</option>
<option value="0">Department A</option>
<option value="1">Department B</option>
<option value="2">Department C</option>
</select>
</span>
</div>
You must now edit the above code to match your array. For example, in your array of emails address which you created in config.php enter the description in the above code. So if the first email address is somedepartment@somedomain.com, enter the department name within the option above replacing ‘Department A’. Do this for each email address you have in your list. Keep the option value as numbers incrementing from 0 (these will correspond the the email array element).
Step Eight: Setting up the Client-Side validation for your new form field
Go-to line 36 in form.php (or there abouts), look for the following line of code:
required.add('answer_out', 'NUMERIC');
Create a new line just below line 36 and enter the following:
required.add('department', 'NOT_EMPTY');
Step Nine:
Save form.php, we are finished with that file.
Step Ten: Setting up the Server-Side validation for your new form field
Open process_form.php in your favorite text/script editor.
Step Eleven:
Go-to line 89 in process_form.php (or there abouts), look for the following line of code:
$reqobj->add("answer_out","NUMERIC");
Create a new line just below line 89 and enter the following:
$reqobj->add("department","NOT_EMPTY");
Step Twelve: Sending the email to the correct department
Go-to line 164 in process_form.php (or there abouts), look for the following line of code:
@mail($email_it_to, $email_subject, $email_message, $headers);
Edit the above to match the following:
$email_item = $_POST['department'];
@mail($email_it_to[$email_item], $email_subject, $email_message, $headers);
Step Thirteen: Saving your files
Save process_form.php.
Now upload all 3 changed scripts, we have made all required changes!
NOTE: Sometimes the code presented in this blog will render slightly wacky (due to the wordpress cms limitations) - please let me know if you spot any errors.