First of all I would like to introduce myself here. My name is Niek, I recently adopted the Silverstripe framework for my new projects. I'm still learning and not the greatest when it comes to coding php, but I'm getting there. Yesterday I stumbled upon a problem I feel I can't solve on my own (I have been hacking away at it for more then 8 hours now..), and that's why I turn to the pro's for help.
Since my hosting provider doesn't support the "normal" mail() function silverstripe uses for email I needed a module that can handle SMTP. I found some on Github, and I decided to use silverstripe-email-helpers from Mark Guinn. I installed the module and it's dependencies via composer, and I followed the instructions about putting
$tls = true; // use tls authentication if true
$charset = 'UTF-8'; // use specified charset if set
// you can specify a port as in 'yourserver.com:587'
$mailer = new SmtpMailer('yourserver.com', 'username', 'password', $tls, $charset);
Email::set_mailer($mailer);
in my mysite/_config.php
Ofcourse I added the information for my webserver in there. I even tested the settings via wormly.com and the testmail came trough. Now for the part where i think the problem lies: implementation. I just need a simple contact form for my website. So I followed the tutorial that is found here on building a simple contact form, and I edited the email part to point to the new smtpmailer function. Anyway, no mails are coming trough.
This is the code:
<?php
class ContactPage extends Page {
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab('Root.Main','ContentLeft');
$fields->removeFieldFromTab('Root.Main','ContentRight');
//$fields->removeFieldFromTab('Root.Main','Content');
return $fields;
}
}
class ContactPage_Controller extends Page_Controller {
private static $allowed_actions = array('Form');
public function ContactForm() {
$fields = new FieldList(
new TextField('Name'),
new EmailField('Email'),
new TextareaField('Message')
);
$actions = new FieldList(
new FormAction('submit', 'Submit')
);
$required = new RequiredFields(array(
'Name', 'Email', 'Message'
));
return new Form($this, 'Form', $fields, $actions, $required);
}
public function submit($data, $form) {
$to = 'info@cibocon.nl';
$from = $data['Email'];
$email = new StyledHtmlEmail(
$from, $to, 'Test Email Task',
'<style>.red { color:red }</style><p>This is a test to see if emails are being sent.
</p><p class="red">This should be red.</p>'
);
$email->send();
return array(
'Content' => '<p>Thank you for your feedback.</p>',
'Form' => ''
);
}
}
Maybe there is something obvious I'm missing here, but being a php-newb I can't wrap my head around it, nor do i have a clue how to get this working. Can somebody help me? It will be much appreciated. I really need a website that can handle emails.