首页 > 解决方案 > 电子邮件联系表单中的 PHP 开关未发送到正确的地址

问题描述

我正在创建一个 HTML 联系表单,该表单使用 php 根据用户所做的选择向 6 个不同的人发送电子邮件。我发送电子邮件的代码,但它只发送到默认值。我不确定需要改变什么才能使开关正常工作。sendTo 正在从 HTML 页面中提取。我把代码放在下面。

// Check if the form has been posted

if (isset($_POST['submit'])) {

    // The email address the email will be sent to
    switch ($_POST['$sendTo'])
    {
    case "membership":
      $sendTo = "email1";
      break;
    case "performance":
      $sendTo = "email2";
      break;
    case "quartets":
      $sendTo = "email3";
      break;
    case "president":
      $sendTo = "email4";
      break;
    case "regional":
      $sendTo = "email5";
      break;
    case "website":
      $sendTo = "email6";
      break;
    default:
      $sendTo = "email6";
    }


    // The email subject
    $subject = "Contact Form Submission";
    // Set the from and reply-to address for the email
    $headers =  'From: Organization <webmaster@organization.com>' . "\r\n" .
                'Reply-To: '.$email."\r\n" .
                'X-Mailer: PHP/' . phpversion();
    // Build the body of the email
    $mailbody = "You have received an email from the POB website contact form.\n\n"
              . "Name: " . $_POST['name'] . "\n"
              . "Email: " . $_POST['email'] . "\n"
              . "Heard From: " . $_POST['howhear'] . "\n"
              . "Message:\n" . $_POST['message'];
    // Send the email
    mail($sendTo, $subject, $mailbody, $headers);
    // Go to the thank you page
    header("location: thankyou.shtml");
    exit;

}


<p><strong>Who do you want to contact?</strong><br/> 
    <select name="sendTo">  
        <option value="none">Select a Contact</option>
        <option value="performance">Performance Manager</option>
        <option value="membership">Membership Chair</option>
        <option value="president">President</option>
        <option value="quartets">Quartet Promotion Chair</option>
        <option value="regional">Regional Events Coordinator</option>
        <option value="website">Website</option>
    </select>
</p>

如果用户在 HTML 页面上选择会员主席,它应该发送到会员电子邮件。其他情况也一样。

标签: phphtml

解决方案


您必须使用表单提交有关类型(成员资格、性能等)的信息,并在 switch 案例中使用此值。假设该变量简称为“sendTo”。(编辑变量名)

switch ($_POST["sendTo"])
    {
    case "membership":
      $sendTo = "email1";
      break;
    case "performance":
      $sendTo = "email2";
      break;
    case "quartets":
      $sendTo = "email3";
      break;
    case "president":
      $sendTo = "email4";
      break;
    case "regional":
      $sendTo = "email5";
      break;
    case "website":
      $sendTo = "email6";
      break;
    default:
      $sendTo = "email6";
    }

推荐阅读