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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Simple Contact Form


Go to End


7 Posts   1609 Views

Avatar
Bagzli

Community Member, 71 Posts

20 May 2014 at 6:20am

Edited: 20/05/2014 6:21am

Hi Guys,

I've been trying to send an e-mail out and followed the official guide however I am not receiving any e-mails. I cannot figure out what is wrong, here is my code:

<?php
class ContactPage extends Page {
}
class ContactPage_Controller extends Page_Controller {
    private static $allowed_actions = array('$Form');
    public function Form() { 
        $fields = new FieldList( 
            new TextField('Name'), 
            new EmailField('Email'), 
            new TextareaField('Message')
        ); 
        $actions = new FieldList( 
            new FormAction('submit', 'Submit') 
        ); 
        $validator = new RequiredFields('Name', 'Message');
        return new Form($this, '$Form', $fields, $actions, $validator); 
    }
    public function submit($data, $form) { 
        $email = new Email(); 
          
        $email->setTo('myemail@mymail.com'); 
        $email->setFrom($data['Email']); 
        $email->setSubject("Contact Message from {$data["Name"]}"); 
          
        $messageBody = " 
            <p><strong>Name:</strong> {$data['Name']}</p> 
            <p><strong>Message:</strong> {$data['Message']}</p> 
        "; 
        $email->setBody($messageBody); 
        $email->send(); 
        return array(
            'Content' => '<p>Thank you for your feedback.</p>',
            'Form' => ''
        );
    }
}

My ContactForm.ss looks like this

$Form

my Page.ss looks like this

$Content
$Form

Note: I changed the e-mail displayed in the code above, but in my actual code I use my real e-mail.

Avatar
bartvanirsel

Community Member, 96 Posts

21 May 2014 at 7:02pm

Does the from email address exist? This was a problem on some of my hosting envoronments.

Avatar
Bagzli

Community Member, 71 Posts

22 May 2014 at 9:30am

yes it does, I've used 2 of my e-mails that I know exist.

Avatar
bartvanirsel

Community Member, 96 Posts

22 May 2014 at 6:58pm

Hi,

Does using a normal php mail function work?
And did you try it on antoher webserver?

Cheers,

Bart

Avatar
Bagzli

Community Member, 71 Posts

23 May 2014 at 12:50pm

I was doing it from a local server and never tried normal one, not sure how the normal one is done.

Avatar
kevinstripe

Community Member, 10 Posts

23 May 2014 at 6:45pm

first you should try using simple PHP mail function, to check whether your SMTP server is working fine....

Avatar
bartvanirsel

Community Member, 96 Posts

2 June 2014 at 7:50pm