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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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   1664 Views

Avatar
neilos

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 http://pastie.org/1337807

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.

Neilos

Avatar
bummzack

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(
		"email1@site.com" => "Report a Student Absence",
		"email2@site.com" => "Contact the Head's PA",
		"email3@site.com" => "Problem with Fronter/VLE",
		"email4@site.com" => "Contact a Member of Staff",
		"email5@site.com" => "General Enquiry",
		"email6@site.com" => "Problem with Parentmail",
		"email7@site.com" => "Other",
	),
	....

Avatar
neilos

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");
{
 $email->setTo('email1@site.com');
}

else
{
 $email->setTo('email2@site.com');
}

Would this approact work better / at all?

Thanks for advice.

Avatar
bummzack

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: http://pastie.org/1338190
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.

Avatar
neilos

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