Email not sending through Silverstripe tutorial form

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!

There could be a few issues at play here.

  1. Have you confirmed your server is capable of sending emails?
  2. Have you checked your servers email logs to see if the operating system is actually receiving the emails and trying to send them? The mail log on your server will reveal this.
  3. You’re sending the email from an email address that you don’t have authority to send as (eg: my-email@gmail.com). Your email server will not be allowed to send email as a GMail account and so it will be rejected by almost every mail server you try to send to. You must send from an email address that you have authority to send from.

Hey thanks for your reply. It turns out I had to make the “from” email an address such as noreply@mydomain.com.

The tutorial is confusing though because it gets you to set up the from address based on the email the user entered (which to my knowledge wouldn’t work in most cases?)