0

I'm receiving a lot of spam when using PHPMailer. Now I would like to set an invisible field and have the script stop (don't sent mail) if the field has a value. I've been searching on this site and found this topic. So I've added it to my code. But when I do this the script doesn't work anymore. Blank page or 500 server error depending where I put the code.

The little piece of code:

if ( !empty($_REQUEST['fooBarBaz']) )
{
    // hidden field was filled out, do something about it
} else {

}

Is there anyone that could tell me where to put this code? I've tried putting it below the opening php tag and close it all the way down. And I've tried placing the else statement before $_SESSION["voornaam"] = $_POST['name']; and close it just before catch (\Exception $e). But nothing seems to work... I just can't seem to get it, still a bit fresh to the whole PHP. Thanks!

Here is my PHPMailer.php file.

    <?php


    /*
    THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION
    */
    session_start();
    require 'PHPMailer-master/PHPMailerAutoload.php';
    if ( !empty($_REQUEST['fooBarBaz']) )
{
    // hidden field was filled out, do something about it
} else {
    /*
    *  CONFIGURE EVERYTHING HERE
    */

    // an email address that will be in the From field of the email.
    $fromEmail = 'info@mysite.nl';
    $fromName = 'My Site';

    // an email address that will receive the email with the output of the form
    $sendToEmail = 'info@mysite.nl';
    $sendToName = 'My Site';

    // subject of the email
    $subject = 'Subject';

    // form field names and their translations.
    // array variable name => Text to appear in the email
    $fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message', 'company' => 'Bedrijfsnaam', 'middlename' => 'Tussenvoegsel');

    // message that will be displayed when everything is OK :)
    $okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';

    // If something goes wrong, we will display this message.
    $errorMessage = 'There was an error while submitting the form. Please try again later';

    $_SESSION["voornaam"] = $_POST['name'];
    $_SESSION["tussenvoegsel"] = $_POST['middlename'];
    $_SESSION["achternaam"] = $_POST['surname'];
    $_SESSION["bedrijfsnaam"] = $_POST['company'];
    $_SESSION["telefoonnummer"] = $_POST['phone'];
    $_SESSION["emailadres_2"] = $_POST['e-mail'];
    $_SESSION["vraag"] = $_POST['message'];
    $_SESSION["aantalpersonen"] = $_POST['group'];
    $_SESSION["voorkeursdatum"] = $_POST['date'];
    $_SESSION['aanhef'] = $_POST['gender'];
    $customerEmail = $_SESSION["emailadres_2"];
    $posttypelink = $_SESSION["get_post_type"];
    $post_type = str_replace('_', '-', $_SESSION["get_post_type"]);

    /*
    *  LET'S DO THE SENDING
    */

    // if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
    error_reporting(E_ALL & ~E_NOTICE);

    try
    {

        if(count($_POST) == 0) throw new \Exception('Form is empty');

        $emailTextHtml .= "<style type='text/css'>body {font-family: Roboto, sans-serif; font-size: 13px;text-align:left; }th{text-align:left!important;}</style>";
        $emailTextHtml .= "<body>";

        $emailTextHtml .= "<table>";
        $emailTextHtml .= "<tr><th>Aantal personen</th><td>".$_POST['group']."</td>";
        $emailTextHtml .= "<tr><th>Voorkeursdatum</th><td>".$_POST['date']."</td>";
        $emailTextHtml .= "</tr></table><table>";
        $emailTextHtml .= "<h2>Klant gegevens</h2>";
        $emailTextHtml .= "<tr><th>Naam</th><td>".$_POST['gender']."&nbsp;".$_POST['name']."&nbsp;".$_POST['middlename']."&nbsp;".$_POST['surname']."</td>";
        $emailTextHtml .= "<tr><th>E-mailadres</th><td>".$_POST['e-mail']."</td>";
        $emailTextHtml .= "<tr><th>Telefoonnummer</th><td>".$_POST['phone']."</td>";
        $emailTextHtml .= "<tr><th>Bedrijfsnaam</th><td>".$_POST['company']."</td>";
        $emailTextHtml .= "<tr><th>Vraag of opmerking</th><td>".$_POST['message']."</td>";
        $emailTextHtml .= "</tr></table>";
        $emailTextHtml .= "</body>";


        $mail = new PHPMailer;
        $mail->CharSet = 'UTF-8';
        $mail->setFrom($fromEmail, $fromName);
        $mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress();
        $mail->addReplyTo($from);

        $mail->isHTML(true);

        $mail->Subject = $subject;
        $mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy

        if(!$mail->send()) {
            throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
        }

        $responseArray = array('type' => 'success', 'message' => $okMessage);
    }
    catch (\Exception $e)
    {
        // $responseArray = array('type' => 'danger', 'message' => $errorMessage);
        $responseArray = array('type' => 'danger', 'message' => $e->getMessage());
    }


    if ($responseArray['type'] == 'success') {

    // success redirect

    header('Location: https://mysite.nl');

    }

    else {

    //error redirect

    header('Location: http://www.example.com/error.html');

    }
    }
    ?>
Jay-oh
  • 426
  • 2
  • 6
  • 28
  • You should enable the error with `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` – executable Jul 10 '18 at 14:11
  • I can't find the part of you code that includes the `if ( ... ) {` part, could you please edit your question to include the part that produces the error? – Epodax Jul 10 '18 at 14:12
  • Edited the question which produces the error... – Jay-oh Jul 10 '18 at 14:15
  • 2
    Copy-pasting code you don't understand **is not** the way to go. – axiac Jul 10 '18 at 14:31
  • You're using an old version of PHPMailer - [get the latest](https://github.com/PHPMailer/PHPMailer) - and though you've wrapped code in a `try` block, you've not told PHPMailer to enable exceptions by passing `true` to the constructor. The errors you're seeing will be shown in your web server's error log, or do what @executable said. – Synchro Jul 10 '18 at 14:36

1 Answers1

0

instead of empty you should check if the array value exitsts:

if ( !isset($_REQUEST['fooBarBaz']) )
{
    // hidden field was filled out, do something about it
} else {

}

if your field comes from a form it doesn't even exist if you don't put a value in. so isset will look if it exists. You are probably getting an error for that reason.

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • Thanks for the suggestion. But I've placed the `!isset` like in the code from above. But I get a 500 internal server error. – Jay-oh Jul 10 '18 at 14:26
  • have a look at the php log for error reporting. Or enable error reporting like said in comments `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` to see what is causing the 500 error (it is a syntax error in your code) – Lelio Faieta Jul 10 '18 at 14:31