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.

All other Modules /

Discuss all other Modules here.

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

Trouble getting the markguinn/silverstripe-email-helpers module to work


Go to End


2 Posts   887 Views

Avatar
Sting2Win

Community Member, 1 Post

4 June 2015 at 11:15pm

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.

Avatar
Pyromanik

Community Member, 419 Posts

5 June 2015 at 10:09am

Edited: 05/06/2015 10:11am

Your form is named wrong.

The paramers are:
1. The controller that has the action on it to get the form(usually $this).
2. The controller action to get to the form
3. fields,
4. actions,
5. validator (e.g. required fields)

The first and second parameters form the submission link.
yoursite.com/ContactPage_Controller/Form

This will be 404 because your form is named ContactForm.
You need yoursite.com/ContactPage_Controller/ContactForm
(Using CMS module this becomes E.g. yoursite.com/contact-us/ContactForm)

An easy away around this is to do Form::create($this, __FUNCTION__, $fileds, $actions, $validator) - then you don't have to worry about form names any more.
http://php.net/manual/en/language.constants.predefined.php