silverstripe/emailing-form

There is no license information available for the latest version (dev-master) of this package.

This module bundles the emailing process into a form object. It also supports the routing of emails to a test address.

dev-master 2015-04-02 11:07 UTC

This package is not auto-updated.

Last update: 2024-04-22 23:25:49 UTC


README

This module bundles the emailing process into a form object. It also lets you route emails to a test address for manual testing of an email form.

Usage

Build the form as usual, but with EmailingForm::create() instead of Form::create(). EmailingForm exposes setting up the email via the form object.

public function ContactForm() {

    // ...

    $form = EmailingForm::create(
        $this, 'ContactForm', $fields, $actions
    );

    $form->setTo('recipient@gmail.com');
    $form->setFrom('Me <sender@gmail.com>');
    $form->setSubject('Hello');
    $form->setEmailTemplate('ContactFormEmail');

    return $form;
}

Then in the form action simply do:

public function SendForm($data, $form){
    $form->Send($data);
}

The Send method will automatically sanitize the data for you before loading it into the template.

Test conditions

This module can be configured to route test emails to a test address for manual form testing. In your main project, configure the EmailTools object in your _config.php:

EmailTools::setTestRouteAddress('emailtest@yourdomain.co.uk');
EmailTools::setTestData(array('emailtest@yourdomain.co.uk'));

Then when you're setting up the form as shown in the usage example do:

$form->setTestCondition(function($e, $data)
{
    return EmailTools::isTestData($data['Email']);
});

The module passes the submitted $data to this callback, if it is set. In this example, we're checking the input data 'Email', however you can check any field you like. If this callback returns true, EmailingForm will route your email to your test address.