# How to make a simple contact form In this how-to, we'll explain how to set up a specific page type holding a contact form, which submits a message via email. Let's start by defining a new `ContactPage` page type: ```php use SilverStripe\Forms\FieldList; use SilverStripe\Forms\TextField; use SilverStripe\Forms\EmailField; use SilverStripe\Forms\TextareaField; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\Form; use Page; use PageController; class ContactPage extends Page { } class ContactPageController extends PageController { private static $allowed_actions = ['Form']; public function Form() { $fields = new FieldList( new TextField('Name'), new EmailField('Email'), new TextareaField('Message') ); $actions = new FieldList( new FormAction('submit', 'Submit') ); return new Form($this, 'Form', $fields, $actions); } } ``` To create a form, we instanciate a `Form` object on a function on our page controller. We'll call this function `Form()`. You're free to choose this name, but it's standard practice to name the function `Form()` if there's only a single form on the page. There's quite a bit in this function, so we'll step through one piece at a time. ```php $fields = new FieldList( new TextField('Name'), new EmailField('Email'), new TextareaField('Message') ); ``` First we create all the fields we want in the contact form, and put them inside a FieldList. You can find a list of form fields available on the [FormField](api:SilverStripe\Forms\FormField) page. ```php $actions = FieldList( new FormAction('submit', 'Submit') ); ``` We then create a [FieldList](api:SilverStripe\Forms\FieldList) of the form actions, or the buttons that submit the form. Here we add a single form action, with the name 'submit', and the label 'Submit'. We'll use the name of the form action later. ```php return new Form($this, 'Form', $fields, $actions); ``` Finally we create the `Form` object and return it. The first argument is the controller that the form is on – this is almost always $this. The second argument is the name of the form – this has to be the same as the name of the function that creates the form, so we've used 'Form'. The third and fourth arguments are the fields and actions we created earlier. To show the form on the page, we need to render it in our template. We do this by appending $ to the name of the form – so for the form we just created we need to add $Form. Add $Form to the themes/currenttheme/Layout/Page.ss template, below $Content. The reason it's standard practice to name the form function 'Form' is so that we don't have to create a separate template for each page with a form. By adding $Form to the generic Page.ss template, all pages with a form named 'Form' will have their forms shown. If you now create a ContactPage in the CMS (making sure you have rebuilt the database and flushed the templates /dev/build?flush=all) and visit the page, you will now see a contact form. ![](../../../_images/howto_contactForm.jpg) Now that we have a contact form, we need some way of collecting the data submitted. We do this by creating a function on the controller with the same name as the form action. In this case, we create the function 'submit' on the ContactPage_Controller class. ```php use SilverStripe\Control\Email\Email; use PageController; class ContactPageController extends PageController { private static $allowed_actions = ['Form']; public function Form() { // ... } public function submit($data, $form) { $email = new Email(); $email->setTo('siteowner@mysite.com'); $email->setFrom($data['Email']); $email->setSubject("Contact Message from {$data["Name"]}"); $messageBody = "
Name: {$data['Name']}
Message: {$data['Message']}
"; $email->setBody($messageBody); $email->send(); return [ 'Content' => 'Thank you for your feedback.
', 'Form' => '' ]; } } ```