SnapShooter Backups Server, Database, Application and Laravel Backups - Get fully protected with SnapShooter

Preview email with DebugKit in CakePHP 3

The ability to preview an email's look and feel during development is a huge time saver for developers.

The de-facto CakePHP development tool DebugKit, shipped along with CakePHP 3, already provides a way to preview an email during development. It renders the email right inside the browser so that we can edit the email view and refresh the browser to get instant feedback.

In this tutorial, we introduce you DebugKit mail panel, a way to preview your email during development without actually sending it.


Email preview only works for CakePHP 3.1+. So you are out of luck if you are in an older version.

Install DebugKit 3+ via composer:

composer require --dev cakephp/debug_kit "~3.0"

Load the plugin via Bake command:

bin/cake plugin load DebugKit

Now you should be able to see DebugKit toolbar showing at the bottom of your application.

If for some reasons, it does not show up. Run the command below, it is to make sure DebugKit's assets(js/css files) can be loaded properly.

bin/cake plugin assets symlink

Create an Email with Mailer

Email preview is implemented with MailPreview class and MailPreview integrates with CakePHP’s Mailer class. Your email needs to be implemented using Mailer class in order to get the preview feature.

Create an email using the Mailer class under src/Mailer/UserMailer:

namespace App\Mailer;
use Cake\Mailer\Mailer;
class UserMailer extends Mailer
    public function welcome($user)
        return $this // Returning the chain is a good idea :)
            ->subject(sprintf("Welcome %s", $user->name))
            ->template("welcome_mail") // By default template with same name as method name is used.
            ->set(["user" => $user]);

If you are not sure how to use Mailer class, take a look at

Create MailPreview

Create a MailPreview class for UserMailer under src/Mailer/Preview/UserMailPreview.php:

namespace App\Mailer\Preview;
use DebugKit\Mailer\MailPreview;
class UserMailPreview extends MailPreview
    public function welcome()
        $user = $this->Users->find()->first();
        return $this->getMailer("User")
            ->set(["activationToken" => "dummy-token"]);

That's all the steps to get the email preview from DebugKit. Now refresh your browser and you should be able to see your emails showing up at the preview panel:


The End

Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.