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

How can I send a cutom form?


Go to End
Reply

14 Posts   4780 Views

Avatar
cliersch

4 July 2008 at 10:03pm (Last edited: 5 July 2008 12:29am), Community Member, 75 Posts

I created a small form - but where and how do I have to implement the email adress and the send template!

function Form() {
return new Form($this, "Form", new FieldSet(
   new TextField("Name", "Name"),
   new EmailField("Email", "E-Mail:")
), new FieldSet(
new FormAction("doform", "send")
));
}
function doform($data, $form) {
}

Please may anybody help me to understand how to send a form with an email.ss template? I could't get it from the tutorial.

Avatar
Sean

4 July 2008 at 11:24pm (Last edited: 5 July 2008 10:45am), Forum Moderator, 921 Posts

Hi there,

Could you please clarify this a bit more so I can understand what you're after?

Do you mean you want to send an email, using a template, once someone submits the form you've made?

If so, you could do something like this:

function doform($data, $form) {
   if(!empty($data['Email'])) {
      $email = new Email_Template(
         'fromemail@somewhere.com',
         $data['Email'],
         'Form was submitted - subject line (change me)'
      );
      $email->ss_template = 'MyEmailTemplate';
      $email->populateTemplate(array(
         'Name' => $data['Name'],
         'Email' => $data['Email']
      ));
      $email->send();
   }
}

Then, you would place MyEmailTemplate.ss, or whatever you have chosen into mysite/templates/email. the populateTemplate() call with an array means you can list in variables that you wish to be available in the template. In this example, $Name and $Email can be used in MyEmailTemplate.ss.

Hope this makes sense.

Cheers,
Sean

Avatar
cliersch

5 July 2008 at 12:18am (Last edited: 5 July 2008 12:29am), Community Member, 75 Posts

Thank you for the code example. I guess thats almost what I need. I try again to explain my problem, I try to install an contact form where the user can fill in his email an name and submit it to my e-mail adress (me@mysite.de) with these given data. (It should work like the UserDefined form - but I need to build it as a php function).
Where do I have to put my email adress?
And I guess I've got a problem with the "if(!empty($data['Email']) { " this ist putting out a

Parse error: syntax error, unexpected '{'

Avatar
Sean

5 July 2008 at 10:40am (Last edited: 5 July 2008 10:41am), Forum Moderator, 921 Posts

Hi there,

The arguments to to the email class are:

new Email_Template('from@email.com', 'to@email.com', 'Subject line');

As for the parse error, there's a missing bracket I forgot to add. That line should instead read:

if(!empty($data['Email'])) {

Avatar
cliersch

5 July 2008 at 10:52am Community Member, 75 Posts

Thanx Sean! I guess now I'm understanding better the logic an how these email forms will work!

Avatar
Willr

6 July 2008 at 1:20pm Forum Moderator, 5511 Posts

see http://doc.silverstripe.com/doku.php?id=email_template for a bit more info on emails

Avatar
Sean

6 July 2008 at 2:10pm Forum Moderator, 921 Posts

willr: FYI, the Email_Template class is deprecated in sapphire/trunk. So, we'll need to update the wiki documentation for when we release 2.2.3.

Avatar
Willr

6 July 2008 at 3:58pm Forum Moderator, 5511 Posts

yea sweet as its just using Email now isnt it?. Versioning for the docs like jquery or cakephp is looking increasingly handy!

Go to Top