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

Erwane CakePHP Libs

Installs: 376

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0


2.0.4 2021-06-21 13:05 UTC

This package is auto-updated.

Last update: 2021-09-21 13:51:50 UTC



Mailer class are mades to simplify the customization of e-mail templates.

Imagine you want to allow your App users or admins to customize theirs e-mails. With this classes, you can set some allowedVars in template and users can use it with {{USER_NAME}} syntax in email.

All vars need to be allowed with setAllowedVars() and are quoted. No injection risk.


use Ecl\Mailer\Mailer;
use Cake\ORM\Entity;

$mailer = new Mailer();
        'user' => new Entity(['name' => 'User Name']),
    ->deliver('Hello {{USER_NAME}}');

This will send a text e-mail with content below:

Hello User Name

This, work with reusable Mailer and templates too.

namespace App\Mailer;

use Ecl\Mailer\Mailer;

class UserMailer extends Mailer
    public function welcome($user)
            ->setSubject(sprintf('Welcome %s', $user->name))
            ->set(['user' => $user]);

// templates/email/html/welcome.php
<p>Hi {{USER_NAME}}</p>

The Mailer add one new method setAllowedVars(array): self, required for template var replacement.


The rendered can be used without Ecl\Mailer\Mailer if you want to pre-fill a wysiwyg textarea.

use Ecl\Mailer\Renderer;

$invoice = $this->Invoices->get(1, ['contain' => ['Customers']]);

// Get template from DB (or templates dir)
/** @var \App\Model\Entity\EmailTemplate $emailTemplate */
$emailTemplate = $this->EmailTemplates
    ->where(['type' => 'invoice_send'])

$body = '';
if ($emailTemplate && $emailTemplate->body) {
    $renderer = new Renderer();
    $rendered = $renderer
            'invoice' => $invoice, 
            'customer' => $invoice->customer,
        ->render($emailTemplate->body, ['html']);

    $body = $rendered['html'];

$body now contain the formatted body e-mail with all allowed vars replaced by values in entities.

<!-- Template -->
<p>Hi {{CUSTOMER_TITLE}}.</p>

    Please find your invoice <b>{{INVOICE_NUM}}<b> attached.

<!-- Body for wysiwyg -->
<p>Hi Company name.</p>

    Please find your invoice <b>FC-202102-01234<b> attached.