How to use this feature?

Testing emails can be very time consuming ⌛

Specially when you need to click a bunch of times on an interface to trigger them.

Wouldn't it be better to just change the templates and refresh the browser to see the result?

Just the way you work on the web interface! 🏃

Example

MailPreview integrates with CakePHP’s Mailer class. Here's an example of such a mailer:

    

    <?php
        
namespace App\Mailer;

        use 
Cake\Mailer\Mailer;

        class 
UserMailer extends Mailer
        
{
            public function 
welcome($user)
            {
                
$mailer $this->setTo($user->email)
                    ->
setSubject(sprintf("Welcome %s"$user->name))
                    ->
setViewVars(["user" => $user]);
                
$mailer->viewBuilder()
                    ->
setTemplate("welcome_mail"// By default template with same name as method name is used.
                    
->setLayout("custom");
                return 
$mailer;
            }
        }

Now you create a MailPreview class where you can pass some dummy values.

    

    <?php
        
// Create the file src/Mailer/Preview/UserMailPreview.php
        
namespace App\Mailer\Preview;

        use 
DebugKit\Mailer\MailPreview;

        class 
UserMailPreview extends MailPreview
        
{
            public function 
welcome()
            {
                
$this->loadModel("Users");
                
$user $this->Users->find()->first();

                return 
$this->getMailer("User")
                    ->
welcome($user)
                    ->
setViewVars(["activationToken" => "dummy-token"]);
            }
        }

Note that the function MUST return the UserMailer object at the end.

Since Mailers have a fluent interface, you just need to return the result of the chain of calls.

That's it, now refresh this page! 🙃