title: How to stop spam messages on contact form - Silverstripe
body: <p>I have a contact form on my website which uses <code>reCAPTCHA version 2</code> to help prevent spam messages. However, quite a few get passed through! How could I stop these? Any help would be greatly appreciated!</p>
  
  <p>Here is the contents of ContactForm.php file:</p>
  
  <pre><code>
  class ContactForm extends Form {
  
      public function __construct(Controller $controller, $name)
      {
          $emails = array(
              'e-mail address here'=&gt;'General',
              'e-mail address here'=&gt;'Sales',
              'e-mail address here'=&gt;'Technical Support',
          );
  
          $fields = FieldList::create(
              TextField::create('Name')-&gt;setAttribute('placeholder', 'Name'),
              EmailField::create('Email')-&gt;setAttribute('placeholder', 'Email'),
              DropdownField::create('Destination', 'Enquiry', $emails)-&gt;setEmptyString("(Department)"),
              TextareaField::create('Message')
                  -&gt;setAttribute('placeholder', 'Message')
                  -&gt;setAttribute('rows', 9)
                  -&gt;setAttribute('cols', 25),
              LiteralField::create('Captcha', '&lt;div class="g-recaptcha" data-sitekey="MY KEY GOES HERE"&gt;&lt;/div&gt;')
          );
  
          $actions = new FieldList(
              FormAction::create('Contact', 'Send')-&gt;setAttribute('disabled',true)
          );
  
          $validator = new RequiredFields('Name', 'Email', 'Message', 'Destination');
          //return Form::create($controller, $name, $fields, $actions, $validator);
          parent::__construct($controller, $name, $fields, $actions, $validator);
      }
  
      public function Contact($data)
      {
          $email = new Email();
          $email-&gt;setFrom($data['Email']);
          $email-&gt;setSubject("Website Enquiry");
          $email-&gt;setTo($data['Destination']);
          $messageBody = "&lt;p&gt;&lt;strong&gt;Name:&lt;/strong&gt; {$data['Name']}&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Message:&lt;/strong&gt; {$data['Subject']}&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Message:&lt;/strong&gt; {$data['Message']}&lt;/p&gt;";
          $email-&gt;setBody($messageBody);
          $email-&gt;send();
  
          Controller::curr()-&gt;redirectBack();
      }
  
  }
  </code></pre>
  
  <p>This is the code ContactPage_Controller.php file:</p>
  
  <pre><code>&lt;?php
  
  class ContactPage_Controller extends Page_Controller {
  
      public static $allowed_actions = array(
          'ContactForm'
      );
  
      public function ContactForm(){
          return ContactForm::create($this, 'ContactForm');
      }
  }
  </code></pre>
  
  <p>This is the contents of ContactPage.ss file:</p>
  
  <pre><code>&lt;div class="service-menu-wrapper"&gt;
      &lt;ul class="services-menu"&gt;
          &lt;% control $ServiceMenu %&gt;
              &lt;li&gt;&lt;a class="hvr-float-shadow internal-link $LinkingMode" href="$Link"&gt;&lt;i class="fa $Icon"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;% end_control %&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  
  &lt;div class="typography container"&gt;
      &lt;div class="content-container js-content"&gt;
  
          &lt;article&gt;
              &lt;div class="custom-title"&gt;
                  &lt;span&gt;Contact us&lt;/span&gt;
              &lt;/div&gt;
  
              &lt;div class="panel"&gt;
                  &lt;div class="section left contact-form-wrapper"&gt;
                      $ContactForm
                  &lt;/div&gt;
                  &lt;div class="section right map"&gt;
  
                      &lt;iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d92480.79610056261!2d-4.0153309783259425!3d52.475305088108385!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xc657c04368dfd9ad!2sAtebol!5e0!3m2!1sen!2suk!4v1526831824458"
                              width="100%" height="450" frameborder="0" style="border:0"
                              allowfullscreen&gt;&lt;/iframe&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/article&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;script&gt;
      $("form").submit(function(event) {
  
          var recaptcha = $("#g-recaptcha-response").val();
          if (recaptcha === "") {
              event.preventDefault();
              alert("Please fill in ReCaptcha");
          }
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>This is the content of ContactPage.php file:</p>
  
  <pre><code>&lt;?php
  
  class ContactPage extends Page
  {
  
      private static $db = array();
  
      private static $has_one = array();
  
      private static $has_many = array();
  
      private static $summary_fields = array();
  
      public function getCMSFields()
      {
          $fields = parent::getCMSFields();
  
          return $fields;
      }
  }
  </code></pre>