0

For some reason when I put my mailer code in an if statement it no longer works. It is detecting whether or not the word hot is entered in a form field. The else statement works fine but when the correct word is entered nothing happens. I cannot figure out why.


    $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>";
}

Here is the code for the form:

<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>
  • What is the content of your $_POST['recipient'] ? – Kozame May 07 '18 at 15:45
  • @Kozame it is the email address that the message is being sent to. The code worked fine before adding the IF ELSE. I am fairly certain it has something to do with that. – austinclamon May 07 '18 at 15:49
  • you should probably edit your post to contain the form – Funk Forty Niner May 07 '18 at 15:51
  • @FunkFortyNiner I just added it. Thanks – austinclamon May 07 '18 at 15:59
  • the input for the bot detect should contain value. How will you know if something/someone entered the word "hot" as per `if ($botdetect == 'hot')`? – Funk Forty Niner May 07 '18 at 16:01
  • 1
    Please narrow it down to a more actionable problem. Email not sending can have a thousand and one causes, see the duplicate. It's impossible for us to tell whether the email is not sending, not getting through, or whether the email sending code isn't being triggered in the first place. Add some `echo` and `var_dump` statements for starters to narrow down what lines of code do and don't get executed and what the values of relevant variables are. – deceze May 07 '18 at 16:02
  • @deceze The problem isnt the sending of the email. The mailer code works perfectly. It is the if else statement that is causing my issues and I am not sure why. I will edit the post to be more clear about this. – austinclamon May 07 '18 at 16:08
  • 1
    Have you tried some simple debugging steps, like adding an `echo "I'M IN THE IF BRANCH NOW!"` every here and there to confirm your code is executing the steps you think it is…? – deceze May 07 '18 at 16:14
  • @deceze Yes. I have confirmed that it is something with the mailer code being in the if statement that is causing the problem. I am just not sure which part.. As I said before the code works fine before being put in an if. – austinclamon May 07 '18 at 16:22
  • Just being inside an `if` statement shouldn't make any difference whatsoever except for some very exceptional cases (mainly `function`/`class` definitions inside the `if`), which I can't see here. – deceze May 07 '18 at 16:24
  • @deceze I just tried creating a function with the mail code and running that in the if. Same problem. Could it have something the do with the USE and REQUIRE_ONCE classes that phpmailer uses? This is racking my brain. – austinclamon May 07 '18 at 18:08

0 Answers0