Ignat
Ignat

Reputation: 1

PHP contact form - dynamic email list based on selected form values

I need to dynamically add multiple email addresses to the Cc: field of the email sent by a contact form. Emails in Cc: should be added based on selected form drop-down values. There are multiple drop-downs in the form, and each value has a number of emails assigned to it.

The recepient of the email is a system that will auto-open a ticket, so the To: field will only have 1 hardcoded email address. But based on other values (paltform and priority), different stakeholders need to be informed that this email has been sent to the system. E.g.:

<tr>
 <td valign="top">
  <label for="priority"> Priority:</label>
</td>
 <td valign="top">
 <select name="priority">
  <option value="3">Normal</option>
  <option value="2">High</option>
  <option value="1">Critical</option>
 </td>
 </select>
 </tr>

 <tr>
 <td valign="top">
  <label for="platform">Platform:</label>
</td>
 <td valign="top">
 <select name="platform">
  <option value="windows">Windows</option>
  <option value="mac">MAC</option>
  <option value="ios">iOS</option>
  <option value="android">Android</option>
 </td>
 </select>
</tr>

If priority=1 (email1,email2,email3) and platform=windows (email4,email5), the Cc: field should have: email1,email2,email3,email4,email5. There are in total 5 drop-downs with 3 to 7 values each, so hardcoding all combinations is not reasonable.

What is the best way of doing it? Is it best to assign a variable to the Cc: header, and then make it fetch emails from pre-defined lists and compose them to a string?

$to = '[email protected]' ;     
$subject = strip_tags($_POST['subject']);
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$_POST['email']." \r\n"; 
$headers .= "Cc: ".$_POST['need to compose this part']." \r\n";

Also, would it be best to save the list of the Cc: emails outside the script, so they can be changed without changing the code?

I'd appreciate any help with this.

Upvotes: 0

Views: 173

Answers (1)

Volodymir
Volodymir

Reputation: 109

Store your email in two arrays: - first array for priority - second array for platform

$arrPriority = array("email1 email2 email3", "email1 email2 email4", "email1 email3 email4");
$arrPlatform = array("windows" => "email5 email6", "mac" => "email5 email7", "ios" => "email6 email7", "android" => "email5 eamil6");
...
$headers .= "Cc: ".$arrPriority[$_POST['priority']]." ".$arrPlatform[$_POST['platform']]." \r\n";

Posting your priority and platform will chose necessary combination of emails Cc:

Upvotes: 1

Related Questions