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.

General Questions

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

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

Simple Contact Form


Reply

7 Posts   411 Views

Avatar
Bagzli

20 May 2014 at 6:20am (Last edited: 20 May 2014 6:21am), Community Member, 37 Posts

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

21 May 2014 at 7:02pm Community Member, 94 Posts

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

Avatar
Bagzli

22 May 2014 at 9:30am Community Member, 37 Posts

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

Avatar
bartvanirsel

22 May 2014 at 6:58pm Community Member, 94 Posts

Hi,

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

Cheers,

Bart

Avatar
Bagzli

23 May 2014 at 12:50pm Community Member, 37 Posts

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

Avatar
kevinstripe

23 May 2014 at 6:45pm Community Member, 10 Posts

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

Avatar
bartvanirsel

2 June 2014 at 7:50pm Community Member, 94 Posts

http://php.net/manual/en/function.mail.php