PHP code example of erwane / cakephp-libs

1. Go to this page and download the library: Download erwane/cakephp-libs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

erwane / cakephp-libs example snippets


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

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

namespace App\Mailer;

use Ecl\Mailer\Mailer;

class UserMailer extends Mailer
{
    public function welcome($user)
    {
        $this
            ->setTo($user->email)
            ->setSubject(sprintf('Welcome %s', $user->name))
            ->setAllowedVars(['USER_NAME'])
            ->set(['user' => $user]);
    }
}

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

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
    ->find()
    ->where(['type' => 'invoice_send'])
    ->first();

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

    $body = $rendered['html'];
}