MJML 的 Symfony 扩展包
Requires
- php: >=7.1.0
- symfony/dependency-injection: ^3.4 || ^4.0 || ^5.0 || ^6.0 || ^7.0
- symfony/process: ^3.4 || ^4.0 || ^5.0 || ^6.0 || ^7.0
- symfony/twig-bundle: ^3.4 || ^4.0 || ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- matthiasnoback/symfony-config-test: ^4.1
- phpstan/phpstan: ^0.12.0
- phpunit/phpunit: ^7.0|^8.0
- symplify/easy-coding-standard: ^9.0
README
使用此扩展包将 MJML 3 和 4 与 Symfony >= 3 结合使用。
安装
使用 Symfony Flex 的应用程序
打开命令行,进入您的项目目录并执行以下命令
$ composer require notfloran/mjml-bundle
未使用 Symfony Flex 的应用程序
步骤 1: 下载扩展包
打开命令行,进入您的项目目录并执行以下命令以下载此扩展包的最新稳定版本
$ composer require notfloran/mjml-bundle
此命令需要您全局安装 Composer,如 Composer 文档中的 安装章节 中所述。
步骤 2: 启用扩展包
然后,通过将其添加到项目中 app/AppKernel.php
文件中注册的扩展包列表中来启用扩展包
// app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new NotFloran\MjmlBundle\MjmlBundle(), ]; // ... } // ... }
渲染器
目前仅提供了一个渲染器,即二进制渲染器。
二进制
安装 MJML
$ npm install mjml
然后您需要更新配置
# config/packages/mjml.yaml mjml: renderer: binary # default: binary options: binary: '%kernel.project_dir%/node_modules/.bin/mjml' # default: mjml node: '/Users/user/.nvm/versions/node/v10.16.0/bin/node' # default: null minify: true # default: false validation_level: skip # default: strict. See https://mjml.io/documentation/#validating-mjml
node
选项是为那些有 $PATH
问题的人准备的,请参阅 #35。
自定义
首先,您必须创建一个实现了 NotFloran\MjmlBundle\Renderer\RendererInterface
的类,然后将其声明为一个服务。
最后,您必须更改配置
# config/packages/mjml.yaml mjml: renderer: 'service' options: service_id: 'App\Mjml\MyCustomRenderer'
PHP 配置
如果您正在使用 symfony 5 并且想用 PHP 文件而不是 YAML 来配置此扩展包
// config/packages/mjml.php <?php declare(strict_types=1); use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $configurator): void { $configurator->extension('mjml', [ 'renderer' => 'binary', 'options' => [ 'binary' => '%kernel.project_dir%/node_modules/.bin/mjml', 'minify' => true, 'validation_level' => 'skip' ] ]); };
API
此扩展包没有与 MJML API 的官方集成。
您可以通过使用 juanmiguelbesada/mjml-php 并按照此 gist: https://gist.github.com/notFloran/ea6bab137be628f6a0c19054e08e6906 来创建自己的集成。
用法
使用 "mjml" twig 标签
{# mail/example.mjml.twig #} {% block email_content %} {% mjml %} <mjml> <mj-body> <mj-section> <mj-column> <mj-image width="100px" src="https://mjml.io/assets/img/logo-small.png"></mj-image> <mj-divider border-color="#F45E43"></mj-divider> <mj-text font-size="20px" color="#F45E43" font-family="helvetica"> Hello {{ name }} from MJML and Symfony </mj-text> </mj-column> </mj-section> </mj-body> </mjml> {% endmjml %} {% endblock %}
public function sendEmail(MailerInterface $mailer) { // The MJMl body is rendered by the mjml tag in the twig file $htmlBody = $this->renderView('templates/mail/example.mjml.twig', ['name' => 'Floran']); $email = (new Email()) ->from('my-app@example.fr') ->to('me@example.fr') ->subject('Hello from MJML!') ->html($htmlBody); $mailer->send($email); // ... }
使用 "mjml" 服务
{# templates/mail/example.mjml.twig #} <mjml> <mj-body> <mj-section> <mj-column> <mj-image width="100px" src="https://mjml.io/assets/img/logo-small.png"></mj-image> <mj-divider border-color="#F45E43"></mj-divider> <mj-text font-size="20px" color="#F45E43" font-family="helvetica"> Hello {{ name }} from MJML and Symfony </mj-text> </mj-column> </mj-section> </mj-body> </mjml>
use NotFloran\MjmlBundle\Renderer\RendererInterface; // ... public function sendEmail(MailerInterface $mailer, RendererInterface $mjml) { $mjmlBody = $this->renderView('templates/mail/example.mjml.twig', ['name' => 'Floran']); $htmlBody = $mjml->render($mjmlBody); $email = (new Email()) ->from('my-app@example.fr') ->to('me@example.fr') ->subject('Hello from MJML!') ->html($htmlBody); $mailer->send($email); // ... }
SwiftMailer 集成
❗ 此集成已弃用,并将从下一个主要版本中删除。
声明以下服务
NotFloran\MjmlBundle\SwiftMailer\MjmlPlugin: tags: [swiftmailer.default.plugin]
创建一个 SwiftMailer 消息,其 MJML 主体(没有 {% mjml %}
)内容类型为 text/mjml
$message = (new \Swift_Message('Hello Email')) ->setFrom('send@example.com') ->setTo('recipient@example.com') ->setBody( $this->renderView('mail/example.mjml.twig'), 'text/mjml' ); $mailer->send($message);
插件将自动渲染 MJML 主体并将主体替换为渲染后的 HTML。
在使用了 spool 的情况下:MJML 内容将被保存在 spool 中,并在 spool 被刷新时进行渲染。
许可证
MjmlBundle 依据 MIT 许可证 授权。