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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

All other Modules /

Discuss all other Modules here.

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

Form, how to set email subject and reciepient from ListboxField

Go to End

5 Posts   1474 Views


Community Member, 19 Posts

1 December 2010 at 11:37pm

Hi All,

I am having some troubles and hope someone can enlighten me.

I have a contact form with a ListboxField. The user chooses an option and based on this option I need to send an email to different people with different subjects.
I have code here

Currently the selection chooses the email reciepient but I am left with a generic subject - $email->setSubject('Website Contact');

Is there any way to have the email subject dynamically chosen aswell? Something like $email->setSubject($data['Subject']); - this obviously will set as the email address.

I'm probabbly approaching from the wrong direction.
Hope I have explained clearly.



Community Member, 904 Posts

2 December 2010 at 12:10am

Sure thing. You can simply add another TextField where you can enter the mail subject.
Your script is very insecure though. It can easily be abused to send mails from any address to any other address... that's going to be abused by spammers in no time.

You should at least check if the mail-to address is actually one from your dropdown... your PHP code seems kinda odd too. In the following snippet, the red parts are completely redundant.

new ListboxField(
	$name = "Subject",
	$title = "Contact Subject",
	$source = array(
		"" => "Report a Student Absence",
		"" => "Contact the Head's PA",
		"" => "Problem with Fronter/VLE",
		"" => "Contact a Member of Staff",
		"" => "General Enquiry",
		"" => "Problem with Parentmail",
		"" => "Other",


Community Member, 19 Posts

2 December 2010 at 12:29am

Thanks for reply, I'm sure you can tell php isn't my forte. I consturcted this from other bits of code.

I'll look into the address check (any pointer where to start looking?)

The ListboxField was used as the subject choice. Adding another TextField to enter the subject takes away the point of having it in the first place.

Maybe if I attempt it from a different point and use the ListboxField to populate the email subject instead of the recipient (would this take away the insecurities?). How can I then select the email recipient based on the subject choice?

along these lines maybe?

if ('Subject' == "Other");


Would this approact work better / at all?

Thanks for advice.


Community Member, 904 Posts

2 December 2010 at 2:05am

You could do this using if/else statements, but that's kinda cumbersome.
I changed your code to use another approach:
In the code you can see, that the email => subject array is a private member of the class. We can now use this for A) populating the dropdown and B) validating the incoming e-mail address. In addition we can also use the array to look up the e-mail subject.

Hope that helps.


Community Member, 19 Posts

2 December 2010 at 4:05am

Wow, wasn't expecting it to be done for me, but that deserves a massive thanks... so THANKS