Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive /

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

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

Selectable recipients on Contact Form


Reply


3 Posts   1804 Views

Avatar
gakenny

Community Member, 153 Posts

12 September 2007 at 10:39pm

Hello,

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.

Cheers,

Gary

Avatar
Sean

Forum Moderator, 922 Posts

13 September 2007 at 12:56am

Edited: 13/09/2007 1:53am

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(
'FirstName',
'Surname',
'Email'
));

// 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';
break;
case 2:
$email = 'someone2@somewhere.com';
break;
case 3:
$email = 'someone3@somewhere.com';
break;
}
}

// 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
$e->populateTemplate($data);

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

// send off the email
$e->send();

// 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
Director::redirectBack();
}

}

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 [url]http://doc.silverstripe.com/doku.php?id=tutorial:3-forms&s=tutorial[/url] 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. :-)

Cheers,
Sean

Avatar
gakenny

Community Member, 153 Posts

13 September 2007 at 10:46am

Thanks Sean,

I'll give that one a go!

Cheers,

Gary