notfloran/mjml-bundle
Composer 安装命令:
composer require notfloran/mjml-bundle
包简介
Symfony bundle for MJML
README 文档
README
Bundle to use MJML 3 and 4 with Symfony >= 3.
Installation
Applications that use Symfony Flex
Open a command console, enter your project directory and execute:
$ composer require notfloran/mjml-bundle
Applications that don't use Symfony Flex
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require notfloran/mjml-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:
// app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new NotFloran\MjmlBundle\MjmlBundle(), ]; // ... } // ... }
Renderer
The default renderer is binary, which uses the official MJML Node.js implementation.
The mjml-php (PHP extension) renderer is also available as an alternative. It is based on MRML, a third-party Rust reimplementation of MJML.
Binary
Install MJML
$ npm install mjml
Then you need to update the configuration:
# 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
The node option is there for those who have problems with $PATH, see #35.
Note that each render spawns a new Node.js process, which may impact performance in high-volume scenarios.
mjml-php
Install mjml-php, a PHP extension that embeds MRML, the MJML rendering engine written in Rust, so that you can render MJML templates directly from PHP without shelling out to a CLI tool.
Then you need to update the configuration:
# config/packages/mjml.yaml mjml: renderer: mjml_php
Custom
First you must create a class which implements NotFloran\MjmlBundle\Renderer\RendererInterface, then declare it as a service.
And finally you have to change the configuration:
# config/packages/mjml.yaml mjml: renderer: 'service' options: service_id: 'App\Mjml\MyCustomRenderer'
PHP Config
If you're using symfony 5 and want to configure this bundle with PHP files instead of 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
The bundle has no official integration with the MJML API.
You can create your own integration by using juanmiguelbesada/mjml-php and following this gist : https://gist.github.com/notFloran/ea6bab137be628f6a0c19054e08e6906.
Usage
Use "mjml" twig tag
{# 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); // ... }
Use "mjml" service
{# 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 integration
❗ This integration is deprecated and will be removed in the next major version.
Declare the following service:
NotFloran\MjmlBundle\SwiftMailer\MjmlPlugin: tags: [swiftmailer.default.plugin]
Create a SwiftMailer message with a MJML body (without {% mjml %}) and with text/mjml as content-type:
$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);
The plugin will automatically render the MJML body and replace the body with the rendered HTML.
In the case where a spool is used: the MJML content is save in the spool and render when the spool is flushed.
License
MjmlBundle is licensed under the MIT license.
notfloran/mjml-bundle 适用场景与选型建议
notfloran/mjml-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.23M 次下载、GitHub Stars 达 114, 最近一次更新时间为 2017 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「email」 「mjml」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 notfloran/mjml-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 notfloran/mjml-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 notfloran/mjml-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Render Twig emails with MJML, the only framework that makes responsive email easy.
Mjml view using mjml over npm
Effortlessly craft responsive email templates using MJML within Laravel Mailables
Easily build emails using PHP and MJML.
Extensible library for building notifications and sending them via different delivery channels.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
统计信息
- 总下载量: 2.23M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 115
- 点击次数: 11
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-09-07