Jump to:

17452 Posts in 4473 Topics by 1971 members


SilverStripe Forums » Archive » Selectable recipients on Contact Form

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1739 Views
  • gakenny
    Community Member
    153 Posts

    Selectable recipients on Contact Form Link to this post


    It would be good to allow a selectable list of a recipient for a contact form. I have a site where we don't want to publish email addresses but we would like visitors to be able to send an email to one of several listed recipients using the online form. At the moment, only one recipient can be set for the form when it is configured. The email address would obviously not be included in the page code.



  • Sean
    Forum Moderator
    921 Posts

    Re: Selectable recipients on Contact Form Link to this post

    What you'll need to do is probably define your own form. The form builder is good for a simple form, but when your requirements get a little more complex it becomes impossible to use. So, we can do something like this:

    [code php]
    class Page extends SiteTree {


    class Page_Controller extends ContentController {

    // create the recipient list, so we can refer back to look up a value
    protected static $recipients = array(
    1 => 'Jim',
    2 => 'Joe',
    3 => 'George'

    public function ContactForm() {

    // define form fields here
    $fields = new FieldSet(
    new TextField('FirstName', 'First name'),
    new TextField('Surname'),
    new EmailField('Email', 'Email address'),
    new DropdownField('Recipient', 'Recipient', self::$recipients)

    // define which fields are required to be filled out      
    $validator = new RequiredFields(array(

    // define the form submit actions here      
    $actions = new FieldSet(
    new FormAction('submit', 'Submit')

    // build the form and return it      
    return new Form($this, 'ContactForm', $fields, $actions, $validator);

    // submit function is run after someone submits the form   
    protected function submit($data, $form) {

    // switch based on recipient array key (as defined in array)
    if($recipients = $data['Recipient']) {
    switch($recipients) {
    case 1:
    $email = 'someone@somewhere.com';
    case 2:
    $email = 'someone2@somewhere.com';
    case 3:
    $email = 'someone3@somewhere.com';

    // create a new email and define template, populate with form data
    $e = new Email_Template();

    // send it to the email defined in the switch statement
    $e->to = $email;

    // from, and subject of the email
    $e->from = 'some@email.com';
    $e->subject = 'A user has submitted the contact form';

    // the .ss email template to use (in templates/email directory)
    $e->ss_template = 'ContactForm_Email';

    // populate the form data into the template

    // populate recipient name into template
    // looks up the user, eg. key 1 value is 'Jim' in the static array
    'Recipient' => self::$recipients[$data['Recipient']]

    // send off the email

    // define a message for the user to see after submission
    $message = 'Thanks for submitting your details.';
    $form->sessionMessage($message, 'good');

    // redirect them back after the process is complete


    We do a switch statement for the dropdown field, which as a recipient list defined in a static array above the function. We populate this dropdownfield with the array values. Once a user submits the form a switch statement prepares the correct email address for sending to. So, for example, 1 => 'Jim' has a case of 1, which allocates someone@somewhere.com to send the email to.

    Calling $ContactForm renders it onto any template. More specifically, it would be ideal on a sub-template in the Layout folder. Anything that extends Page, or you can put the functions on a subclass of Page if you have any.

    $e->ss_template = 'ContactForm_Email' defines the email template which gets sent, and is populated using $e->populateTemplate() with all the form data, so you can call $FirstName, or $Email inside ContactForm_Email.ss (in templates/email).

    Read up on http://doc.silverstripe.com/doku.php?id=tutorial:3-forms&s=tutorial for more information on the process of creating a form from scratch in SilverStripe. Note that I jotted down this code in about 15 minutes so it's possible some things don't work, but it could be a good start. Let me know if there's anything not working, or you don't understand.


  • gakenny
    Community Member
    153 Posts

    Re: Selectable recipients on Contact Form Link to this post

    Thanks Sean,

    I'll give that one a go!



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.