0

I've set up a PHP e-mail form and everything works fine. However, I'm not sure how to make it so different subject selections will send to different e-mail addresses. Could anyone please help me? Thank you.

HTML:

        <label><strong>Subject:</strong></label>
        <select name="subject" size="1">
            <option value="General Feedback">General Feedback</option>
            <option value="Book Information">Book Information</option>
            <option value="Business Inquiries">Business Inquiries</option>
            <option value="Website Related">Website Related</option> 
        </select>

PHP:

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "The Official Website of Ricky Tsang <ricky@rickytsang.ca>";
    $email_subject = $_REQUEST['subject'];

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['full_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['subject']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

    $full_name = $_POST['full_name'];
    $email_from = $_POST['email'];
    $subject = $_POST['subject'];
    $message= $_POST['message'];

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The e-mail you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$full_name)) {
    $error_message .= 'The name you entered does not appear to be valid.<br />';
  }
  if(strlen($message) < 2) {
    $error_message .= 'The message you entered doee not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Full Name: ".clean_string($full_name)."\n";
    $email_message .= "E-mail: ".clean_string($email_from)."\n";
    $email_message .= "Subject: ".clean_string($subject)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";
    $email_from = $full_name.'<'.$email_from.'>';

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->

Thank you for contacting The Official Website of Ricky Tsang. We will be in touch with you very soon.

<?php
}
?>
DearRicky
  • 402
  • 2
  • 6
  • 16
  • Just remember that validating an email address using regular expressions is a tricky business. http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses – Gary Willoughby Jul 18 '11 at 19:44

1 Answers1

3

May I suggest something like this:

HTML Snippet

You could construct the select using the options in the array below if you wanted.

<label>
  <strong>Subject:</strong>
</label>
<select name="subject" size="1">
  <option value="1">General Feedback</option>
  <option value="2">Book Information</option>
  <option value="3">Business Inquiries</option>
  <option value="4">Website Related</option> 
</select>

PHP Snippet

<?php

$subjects = array(
  1 => array(
    'to' => 'user@example.org',
    'subject' => 'General Feedback'
  ),
  2 => array(
    'to' => 'user@example.net',
    'subject' => 'Book Information'
  ),
  3 => array(
    'to' => 'user@example.com',
    'subject' => 'Business Inquiries'
  ),
  4 => array(
    'to' => 'anotheruser@example.org',
    'subject' => 'Website Related'
  )
);

$email_to = ! empty($subjects[$_REQUEST['subject']]['to']) ? $subjects[$_REQUEST['subject']]['to'] : 'default@example.com';
$email_subject = ! empty($subjects[$_REQUEST['subject']]['subject']) ? $subjects[$_REQUEST['subject']]['subject'] : 'Unknown subject';
Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • Thanks, but I tried this and I'm not receiving e-mail anymore? Is there a special place where I should put this? – DearRicky Jul 18 '11 at 19:05
  • @DearRicky - The HTML goes in your form, in place of the snippet you posted above and the PHP snippet replaces the 2 lines after `if(isset($_POST['email'])) {` in your PHP snippet above. – Francois Deschenes Jul 18 '11 at 19:17
  • All right. I figured it out, but it's messing up my script in that I'm not getting the subject line I need. Please take a look at my last solved question: http://stackoverflow.com/questions/6728361/php-e-mail-form-sender-name-instead-of-e-mail – DearRicky Jul 18 '11 at 19:24
  • And it's only sending e-mail to one address. – DearRicky Jul 18 '11 at 19:33