电子邮件联系表单中的PHP开关未发送到正确的地址

时间:2019-04-29 12:30:47

标签: php html

我正在创建一个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页面上选择了Membership Chair,它将发送到会员电子邮件。其他情况也一样。

2 个答案:

答案 0 :(得分:2)

您必须使用表单提交有关类型(成员资格,性能等)的信息,并在转换案例中使用此值。 可以说该变量简称为“ 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";
    }

答案 1 :(得分:0)

您的代码中有错字。您尝试获取POST信息“ $ sendTo”,但实际信息为$_POST['sendTo']

switch($_POST['sendTo']){
  case "membership":
    $sendTo = "email1..";
    break;
  ...
}

在PHP中,$用来表示以下内容是变量,但是在您的情况下,您已经使用了$ _POST变量,其中包含有关活动POST请求的信息。在$ _POST内部是一个通过$_POST['sendTo']访问的信息,其中包含在HTML表单中选择的值。