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.

Archive /

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

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

Getting a contact form to email properly


Go to End


2 Posts   1881 Views

Avatar
Andrew Houle

Community Member, 140 Posts

11 November 2008 at 4:54am

I've been studying how to create a customized contact form using these forum discussions: http://www.silverstripe.com/site-builders-forum/flat/53148 and http://www.silverstripe.com/site-builders-forum/flat/113907?showPost=164944. My simple form works to save the data in a database, but I'm struggling to send the data to the site owner. I'm not sure where to put my template file MyEmail.ss, and what exactly goes in it? And, do I need a MyEmail.php class file that extends Email.php. If so, what goes in that and where would I place that? Here is my code so far:

<?php

class ContactUsPage extends Page {


}

class ContactUsPage_Controller extends Page_Controller {

	function ContactUsForm() {
		return new Form($this, "ContactUsForm", new FieldSet(
 
			// List your fields here
			new TextField("Name", "Name"),
			new EmailField("Email", "Email Address"),
			new TextareaField("Message", "Message")
 
		), new FieldSet(
 
			// List the action buttons here
			new FormAction("SubmitContactUs", "Submit")
 
		), new RequiredFields(
 
			// List the required fields here: "Email", "FirstName"
			Email,
			Name,
			Message
 
		));
	}
	
	 function SubmitContactUs( $data, $form ) {
      	$submittedForm = new ContactData();      
      	$form->saveInto($submittedForm); 
	  	$submittedForm->write();
	
		$email = new MyEmail;
      	$email->populateTemplate($submittedForm);
      	$email->send(); 

	
	 	Director::redirect('cp/');

	 }

	
	
} 

?>

Avatar
Liam

Community Member, 470 Posts

11 November 2008 at 6:05am

The email templates go in mysite/templates/email/MyEmail.ss

Setup this up as an html document type and loop through the data with something like <% control FormData %>variables here <% end_control %>