Hey guys, I’m on Silverstripe 4 (on a live site). I’ve set up a contact form with validation and captcha, everything appears to be working except the email doesn’t get sent. I’ve used the basic contact form tutorial Silverstripe has provided. Here’s my code below in the PageController:
<?php
namespace {
use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\View\Requirements;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\EmailField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Control\Email\Email;
class PageController extends ContentController
{
private static $allowed_actions = ['ContactForm'];
// private static $allowed_actions = ['submit'];
public function ContactForm()
{
$fields = new FieldList(
TextField::create('Name','')->setAttribute('placeholder', 'Your name'),
EmailField::create('Email', '')->setAttribute('placeholder', 'Your email'),
TextareaField::create('Message', '')->setAttribute('placeholder', 'Your Message')
);
$actions = new FieldList(
new FormAction('submit', 'Submit')
);
$validator = new RequiredFields('Name', 'Email', 'Message');
$form = Form::create($this, 'ContactForm', $fields, $actions, $validator);
$form->enableSpamProtection();
return $form;
}
public function submit($data, $form)
{
$email = new Email();
$email->setTo('stannard.tim@gmail.com');
$email->setFrom($data['Email']);
$email->setSubject("Contact Message from {$data["Name"]} | TS Portfolio");
$messageBody = "
<p><strong>Name:</strong> {$data['Name']}</p>
<p><strong>Message:</strong> {$data['Message']}</p>
";
$email->setBody($messageBody);
$email->send();
return [
'ContactForm' => 'Submitted successfully. Thank you for your message!'
];
}
/**
* An array of actions that can be accessed via a request. Each array element should be an action name, and the
* permissions or conditions required to allow the user to access it.
*
* <code>
* [
* 'action', // anyone can access this action
* 'action' => true, // same as above
* 'action' => 'ADMIN', // you must have ADMIN permissions to access this action
* 'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
* ];
* </code>
*
* @var array
*/
// private static $allowed_actions = [];
protected function init()
{
parent::init();
Requirements::css('themes/portf/css/bootstrap.min.css');
Requirements::css('themes/portf/css/animate.css');
Requirements::css('themes/portf/css/font-awesome.min.css');
Requirements::css('themes/portf/css/style.css');
Requirements::javascript('themes/portf/js/jquery.js');
}
}
}
I’ve tried replacing the submit function to just send a test email like this on submit but this doesn’t work as well:
$email = new Email('test@email.com', 'my-email@gmail.com', 'test subject','test body');
$email->send();
Any help is appreciated thanks!