首页 > 解决方案 > 放入 if 语句时代码突然不起作用

问题描述

出于某种原因,当我将邮件代码放入 if 语句时,它不再起作用。它正在检测是否在表单字段中输入了 hot 一词。else 语句工作正常,但是当输入正确的单词时没有任何反应。我不知道为什么。


    $botdetect = $_POST['botdetect'];
if ($botdetect == 'hot') {

  $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
  try {
      //Server settings
      $mail->SMTPDebug = 0;                                 // Enable verbose debug output
      $mail->isSMTP();                                      // Set mailer to use SMTP
      $mail->Host = '-----';              // Specify main and backup SMTP servers
      $mail->SMTPAuth = true;                               // Enable SMTP authentication
      $mail->Username = '-----';   // SMTP username
      $mail->Password = '------';                         // SMTP password
      $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
      $mail->Port = 587;                                    // TCP port to connect to

      //Recipients
      $mail->setFrom('------', '------');

      $mail->AddAddress($_POST['recipient']);               // Add a recipient

      //$mail->addCC('cc@example.com');                     //Add if needed
      //$mail->addBCC('bcc@example.com');

      //Attachments
      //$mail->addAttachment('/var/tmp/file.tar.gz');       // Add attachments
      //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');  // Optional name

      $mail->Subject = 'You Have A Message!';               //Subject Line

      // Retrieve the email template required
      $message = file_get_contents('template/template.html');

      // Replace the % with the actual information
      $message = str_replace('%name%', $_POST['name'], $message);
      $message = str_replace('%email%', $_POST['email'], $message);
      $message = str_replace('%phone%', $_POST['phone'], $message);
      $message = str_replace('%message%', $_POST['message'], $message);

      //Set the message
      $mail->MsgHTML($message);

      $mail->send();
      echo "<script type='text/javascript'>alert('Message Sent!');</script>";

  } catch (Exception $e) {
      echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
  }


}
else {
  echo "<script type='text/javascript'>alert('Bot Detected!');</script>";
}

这是表单的代码:

<body>
  <!-- iframe and form method, action, and target must be the same as shown here-->
  <iframe name="sugar-mail" style="display: none;"></iframe>
  <form method="post" action="mail.php" target="sugar-mail">
    <!--input with id recipient is needed to send the email. Obfuscated address to prevent spam.-->
    <script>var x=document.createElement("INPUT");x.setAttribute("id","recipient"),x.setAttribute("name","recipient"),x.setAttribute("type","hidden"),x.setAttribute("value","a&#117;stin&#64;sugar-&#114;ock&#46;&#99;om"),document.body.appendChild(x);</script>

    Email: <input name="email" id="email" type="text" required /><br /> Message:

    <br />
    <textarea name="message" id="message" rows="5" cols="40" required></textarea><br />

    <!--Bot Detection -->
    Is fire hot or cold? <input name="botdetect" id="botdetect" type="text" required />

<br />
    <input type="submit" value="Submit" />
  </form>
</body>

标签: phphtmlphpmailer

解决方案


推荐阅读