Jump to:

22977 Posts in 11742 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Contact Page Type

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1929 Views
  • Brady.Dyer
    Avatar
    Community Member
    21 Posts

    Contact Page Type Link to this post

    Does anyone have a page type they can post up that has a contact form that has some form of spam protection on it?

    I know you can easily enable the math protection to comments on pages, but not for a CMS-generated form.

    If anyone has anything that gets around spam problems with forms created in the CMS, that would be great if you could share =)

  • UncleCheese
    Avatar
    4085 Posts

    Re: Contact Page Type Link to this post

    I use this on all my sites. Not the sexiest thing in the world, but it works.

    /mysite/code/ContactPage.php

    <?php

    class ContactPage extends Page
    {
       static $db = array (
          'SuccessMessage' => 'HTMLText'
       );
       
       public function getCMSFields()
       {
          $f = parent::getCMSFields();
          $f->addFieldToTab("Root.Content.Success Message", new HTMLEditorField('SuccessMessage','Success message (to be displayed after a user submits the form'));
          return $f;
       }
       

    }

    class ContactPage_Controller extends Page_Controller
    {
       public function ContactForm()
       {
          return new Form(
             $this,
             'ContactForm',
             new FieldSet(
                new TextField('Name','Your name'),
                new EmailField('Email','Your email address'),
                new TextField('Spam','Spam protection: <em>Is fire hot or cold?</em>'),
                new TextareaField('Message','Message','10','40')
             ),
             new FieldSet(
                new FormAction('doContactSubmit','Submit')
             ),
             new RequiredFields(
                'Message','Spam'
             )
          );
       }
       
       public function doContactSubmit($data,$form)
       {
          $data = $form->getData();
          if(trim(strtolower($data['Spam'])) != 'hot') {
             Director::redirect($this->Link('spam'));
          }
          else {
             $email = new Email();
             $email->to = 'myemail@addressl.com';
             $email->subject = 'My Website: Contact Form';
             $email->from = !empty($data['Email']) ? $data['Email'] : 'webmaster@mysite.com';
             $email->ss_template = "ContactPageEmail";
             $email->populateTemplate($data);
             $email->send();
             Director::redirect($this->Link('success'));
          }

       }
       
       public function Successful()
       {
          $p = Director::urlParams('Action');
          return isset($p['Action']) && $p['Action'] == 'success';
             
       }

       public function SpamError()
       {
          $p = Director::urlParams('Action');
          return isset($p['Action']) && $p['Action'] == 'spam';
             
       }

    }

    ?>

    /mysite/templates/Layout/ContactPage.ss

    <div id="content">
       <% if Successful %>
          $SuccessMessage
       <% else_if SpamError %>
          <p>Sorry, you did not provide a valid answer to the spam protection.</p>
          $ContactForm
       <% else %>
          $Content
          $ContactForm
       <% end_if %>
    </div>

    /mysite/templates/email/ContactPageEmail.ss

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>

    <head>
    </head>
    <body>

    <p class="body">
    A user has submitted a contact form through your website. His/her info appears below.
    </p>
    <p class="body">
    <strong>Name</strong>: $Name<br />
    <strong>Email</strong>: $Email<br />
    <strong>Message</strong>:
    </p>
    <p class="body">
    $Message
    </p>

    </body>
    </html>

  • Brady.Dyer
    Avatar
    Community Member
    21 Posts

    Re: Contact Page Type Link to this post

    Thanks for that =)

    Have tried to get it to work, but there is an error when it tries to send out the email. Do i need to change anything else here:

    $email = new Email();
    $email->to = 'noreply@beautyworx.co.nz';
    $email->subject = 'Beautyworx: Contact Form';
    $email->from = !empty($data['Email']) ? $data['Email'] : 'noreply@beautyworx.co.nz';
    $email->ss_template = "ContactPageEmail";
    $email->populateTemplate($data);
    $email->send();
    Director::redirect($this->Link('success'));

  • Brady.Dyer
    Avatar
    Community Member
    21 Posts

    Re: Contact Page Type Link to this post

    Am getting the following error:

    FATAL ERROR: Missing argument 1 for Email::__construct(), called in /home/duplico/beautyworx/dev/beautyworx/code/ContactPage.php on line 48 and defined
    At line 52 in /home/duplico/beautyworx/dev/sapphire/core/Email.php

    Think it might have something to do with it not passing the forms details to the template and hence thinking the from field is blank, because if I change

    $email->from = !empty($data['Email']) ? $data['Email'] : 'webmaster@mysite.com';

    to

    $email->from = 'webmaster@mysite.com';

    it sends it - but then the contents of the email I get are empty - as if the template sending the email isnt getting the data correctly

    1929 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.