erwane/cakephp-libs 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

erwane/cakephp-libs

最新稳定版本:3.0.0

Composer 安装命令:

composer require erwane/cakephp-libs

包简介

Erwane CakePHP Libs

README 文档

README

Software License codecov Build Status Packagist Downloads Packagist Version

Version map

branch CakePHP core PHP min
1.x ^3.0 PHP 7.2
2.x ^4.0 PHP 7.4
3.x ^5.0 PHP 8.1

Mailer

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.

Mailer

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}}');

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)
    {
        $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>

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

Renderer

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
    ->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'];
}

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

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

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

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

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

统计信息

  • 总下载量: 1.45k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-12-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固