【问题标题】:PHP E-mail Form: Different E-mails For Dropdown MenuPHP 电子邮件表单:下拉菜单的不同电子邮件
【发布时间】:2011-10-07 09:40:41
【问题描述】:

我已经设置了一个 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.\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
}
?>

【问题讨论】:

标签: php html css


【解决方案1】:

我可以建议这样的事情吗:

HTML 片段

如果需要,您可以使用下面数组中的选项构造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';

【讨论】:

  • 谢谢,但我试过了,但我不再收到电子邮件了?有什么特别的地方应该放这个吗?
  • @DearRicky - HTML 进入您的表单,代替您在上面发布的 sn-p,PHP sn-p 替换了上面 PHP sn-p 中 if(isset($_POST['email'])) { 之后的两行。
  • 好的。我想通了,但它弄乱了我的脚本,因为我没有得到我需要的主题行。请看一下我最后解决的问题:stackoverflow.com/questions/6728361/…
  • 而且它只向一个地址发送电子邮件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多