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']." ".$_POST['name']." ".$_POST['middlename']." ".$_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');
}
}
?>