Jump to:

10997 Posts in 2731 Topics by 1822 members

All other Modules

SilverStripe Forums » All other Modules » Form, how to set email subject and reciepient from ListboxField

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 984 Views
  • neilos
    Avatar
    Community Member
    19 Posts

    Form, how to set email subject and reciepient from ListboxField Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Form, how to set email subject and reciepient from ListboxField Link to this post

    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",
       ),
       ....

  • neilos
    Avatar
    Community Member
    19 Posts

    Re: Form, how to set email subject and reciepient from ListboxField Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Form, how to set email subject and reciepient from ListboxField Link to this post

    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.

  • neilos
    Avatar
    Community Member
    19 Posts

    Re: Form, how to set email subject and reciepient from ListboxField Link to this post

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

    984 Views
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.