PHP电子邮件表单:不同的电子邮件下拉菜单



我已经设置了一个PHP电子邮件表单,一切工作正常。然而,我不确定如何使不同的主题选择将发送到不同的电子邮件地址。有人能帮帮我吗?谢谢你。

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.nn";
    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."rn".
'Reply-To: '.$email_from."rn" .
'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
}
?>

我可以这样建议吗:

<<p> HTML片段/strong>

如果你愿意,你可以使用下面数组中的选项来构造select

<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代码片段

<?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';

最新更新