spatie/mjml-php 问题修复 & 功能扩展

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

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

spatie/mjml-php

Composer 安装命令:

composer require spatie/mjml-php

包简介

Convert MJML to HTML using PHP

README 文档

README

Latest Version on Packagist Tests Total Downloads

MJML is a markup language designed to reduce the pain of coding a responsive email. Our mjml-php package can convert MJML to HTML.

Here's an example of how to use our package:

use Spatie\Mjml\Mjml;

$mjml = <<<'MJML'
    <mjml>
      <mj-body>
        <mj-section>
          <mj-column>
            <mj-text invalid-attribute>Hello World</mj-text>
          </mj-column>
        </mj-section>
      </mj-body>
    </mjml>
    MJML;

$html = Mjml::new()->toHtml($mjml);

The returned HTML will look like the HTML in this snapshot file (it's a bit too large to inline in this readme). Most email clients will be able to render this HTML perfectly.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/mjml-php

In your project, or on your server, you must have the JavaScript package mjml installed.

npm install mjml

... or Yarn.

yarn add mjml

Make sure you have installed Node 16 or higher.

Usage

The easiest way to convert MJML to HTML is by using the toHtml() method.

use Spatie\Mjml\Mjml;

// let's assume $mjml contains the MJML you want to convert

$html = Mjml::new()->toHtml($mjml);

If the MJML could not be converted at all a Spatie\Mjml\Exceptions\CouldNotRenderMjml exception will be thrown.

Using convert()

The toHtml() method will just return the converted HTML. There's also a convert() method that will return an instance of Spatie\Mjml\MjmlResult that contains the converted HTML and some metadata.

use Spatie\Mjml\Mjml;

// let's assume $mjml contains the MJML you want to convert

$result = Mjml::new()->convert($mjml); // returns an instance of Spatie\Mjml\MjmlResult

On the returned instance of Spatie\Mjml\MjmlResult you can call the following methods:

  • html(): returns the converted HTML
  • array(): returns a structured version of the given MJML
  • hasErrors(): returns a boolean indicating if there were errors while converting the MJML
  • errors(): returns an array of errors that occurred while converting the MJML

The errors() method returns an array containing instances of Spatie\Mjml\MjmlError. Each Spatie\Mjml\MjmlError has the following methods:

  • line(): returns the line number where the error occurred
  • message(): returns the error message
  • formattedMessage(): returns the error message with the line number prepended
  • tagName(): returns the name of the tag where the error occurred

Customizing the rendering

There are various methods you can call on the Mjml class to customize the rendering. For instance the minify() method will minify the HTML that is returned.

use Spatie\Mjml\Mjml;

// let's assume $mjml contains the MJML you want to convert
$minifiedHtml = Mjml::new()->minify()->toHtml($mjml);

These are all the methods you can call on the Mjml class:

  • minify(): minify the HTML that is returned
  • beautify(): beautify the HTML that is returned
  • hideComments(): hide comments in the HTML that is returned
  • validationLevel(ValidationLevel $validationLevel): set the validation level to strict, soft or skip

Instead of using these dedicated methods, you could opt to pass an array with options as the second argument of the toHtml or convert method. You can use any of the options that are mentioned in the MJML documentation for Node.js.

use Spatie\Mjml\Mjml;

// let's assume $mjml contains the MJML you want to convert
$minifiedHtml = Mjml::new()->minify()->toHtml($mjml, [
    'beautify' => true,
    'minify' => true,
]);

Validating MJML

You can make sure a piece of MJML is valid by using the canConvert() method.

use Spatie\Mjml\Mjml;

Mjml::new()->canConvert($mjml); // returns a boolean

If true is returned we'll be able to convert the given MJML to HTML. However, there may still be some errors while converting the MJML to HTML. These errors are not fatal and the MJML will still be converted to HTML. You can see these non-fatal errors when calling errors() on the MjmlResult instance that is returned when calling convert.

You can use canConvertWithoutErrors to make sure the MJML is both valid and that there are no non-fatal errors while converting it to HTML.

use Spatie\Mjml\Mjml;

Mjml::new()->canConvertWithoutErrors($mjml); // returns a boolean

Specifying the path to nodejs executable

By default, the package itself will try to determine the path to the node executable. If the package can't find a path, you can specify a path in the environment variable MJML_NODE_PATH

MJML_NODE_PATH=/home/user/.nvm/versions/node/v20.11.0/bin

Sidecar

This package also supports running through Sidecar in Laravel projects.

To use the ->sidecar() method, a few extra steps are needed:

Install the Sidecar package:

composer require spatie/mjml-sidecar

Register the MjmlFunction in your sidecar.php config file.

/*
 * All of your function classes that you'd like to deploy go here.
 */
'functions' => [
    \Spatie\MjmlSidecar\MjmlFunction::class,
],

Deploy the Lambda function by running:

php artisan sidecar:deploy --activate

See the Sidecar documentation for details.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

spatie/mjml-php 适用场景与选型建议

spatie/mjml-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 906.7k 次下载、GitHub Stars 达 277, 最近一次更新时间为 2023 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「spatie」 「mjml-php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 spatie/mjml-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 spatie/mjml-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 spatie/mjml-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 906.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 277
  • 点击次数: 13
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 277
  • Watchers: 4
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-21