dingo-d/php-mjml-renderer 问题修复 & 功能扩展

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

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

dingo-d/php-mjml-renderer

Composer 安装命令:

composer require dingo-d/php-mjml-renderer

包简介

PHP renderer for MJML language

README 文档

README

CI Checks PHP Version GitHub License

A pure PHP implementation of the MJML rendering engine. Convert MJML markup to responsive HTML emails without requiring Node.js or external APIs.

Why?

Existing PHP MJML libraries rely on either:

  • The official MJML API (requires external service)
  • Node.js executable (requires Node.js installation)

This library is a pure PHP implementation that renders MJML to HTML entirely in PHP, with no external dependencies beyond PHP itself.

Features

Complete MJML Support

All 27 standard MJML elements are fully implemented:

Head Components:

  • <mj-head> - Document head wrapper
  • <mj-title> - Email title
  • <mj-preview> - Preview text
  • <mj-font> - External font imports
  • <mj-style> - Custom CSS styles
  • <mj-breakpoint> - Responsive breakpoint configuration
  • <mj-attributes> - Global attribute defaults
  • <mj-html-attributes> - Custom HTML attributes

Layout Components:

  • <mj-body> - Email body wrapper
  • <mj-section> - Horizontal sections
  • <mj-column> - Columns within sections
  • <mj-wrapper> - Full-width background wrapper
  • <mj-group> - Non-stacking column groups
  • <mj-hero> - Hero image with overlay content

Content Components:

  • <mj-text> - Text content
  • <mj-button> - Call-to-action buttons
  • <mj-image> - Responsive images
  • <mj-divider> - Horizontal dividers
  • <mj-spacer> - Vertical spacing
  • <mj-table> - HTML tables with MJML styling

Interactive Components:

  • <mj-accordion> + children - Collapsible FAQ sections
  • <mj-carousel> + children - Image carousels
  • <mj-navbar> + children - Navigation menus
  • <mj-social> + children - Social media icons

Utility Components:

  • <mj-raw> - Raw HTML passthrough
  • <mj-include> - File inclusion

Modern PHP

  • PHP 8.1+ with strict types
  • PSR-12 coding standards
  • PHPStan level 9 static analysis
  • Comprehensive test coverage

Performance

Excellent rendering performance:

  • Simple emails: ~0.3ms average
  • Complex emails: ~1.2ms average
  • Large emails (10+ sections): ~2.8ms average
  • Parsing: ~0.03ms average

Installation

composer require dingo-d/php-mjml-renderer

Usage

Basic Example

<?php
require_once 'vendor/autoload.php';

use MadeByDenis\PhpMjmlRenderer\Renderer\MjmlRenderer;

$renderer = new MjmlRenderer();

$mjml = <<<'MJML'
<mjml>
    <mj-body>
        <mj-section>
            <mj-column>
                <mj-text font-size="20px" color="#F45E43" font-weight="700">
                    Welcome to Our Service!
                </mj-text>
                <mj-button background-color="#F45E43" href="https://example.com">
                    Get Started
                </mj-button>
            </mj-column>
        </mj-section>
    </mj-body>
</mjml>
MJML;

$html = $renderer->render($mjml);

Newsletter Example

$mjml = <<<'MJML'
<mjml>
    <mj-body>
        <mj-section background-color="#f0f0f0">
            <mj-column>
                <mj-image src="https://example.com/logo.png" width="200px"></mj-image>
                <mj-text align="center" font-size="24px" font-weight="700">
                    Monthly Newsletter
                </mj-text>
                <mj-divider border-color="#333"></mj-divider>
            </mj-column>
        </mj-section>

        <mj-section>
            <mj-column width="50%">
                <mj-image src="https://example.com/article1.jpg"></mj-image>
                <mj-text font-weight="700">Featured Article 1</mj-text>
                <mj-text>Article description goes here...</mj-text>
                <mj-button href="https://example.com/article1">Read More</mj-button>
            </mj-column>
            <mj-column width="50%">
                <mj-image src="https://example.com/article2.jpg"></mj-image>
                <mj-text font-weight="700">Featured Article 2</mj-text>
                <mj-text>Article description goes here...</mj-text>
                <mj-button href="https://example.com/article2">Read More</mj-button>
            </mj-column>
        </mj-section>
    </mj-body>
</mjml>
MJML;

$html = $renderer->render($mjml);

Hero Image Example

$mjml = <<<'MJML'
<mjml>
    <mj-body>
        <mj-hero
            mode="fixed-height"
            height="400px"
            background-url="https://example.com/hero.jpg"
            background-color="#2a2a2a"
        >
            <mj-text
                color="#ffffff"
                font-size="40px"
                align="center"
                font-weight="700"
            >
                Summer Sale
            </mj-text>
            <mj-button background-color="#ff6600" href="https://example.com/shop">
                Shop Now
            </mj-button>
        </mj-hero>
    </mj-body>
</mjml>
MJML;

$html = $renderer->render($mjml);

Multi-Column Layout

$mjml = <<<'MJML'
<mjml>
    <mj-body>
        <mj-section>
            <mj-column width="33.333%">
                <mj-image src="https://example.com/feature1.png"></mj-image>
                <mj-text align="center" font-weight="700">Fast Delivery</mj-text>
                <mj-text align="center">Get your order in 2 days</mj-text>
            </mj-column>
            <mj-column width="33.333%">
                <mj-image src="https://example.com/feature2.png"></mj-image>
                <mj-text align="center" font-weight="700">Free Returns</mj-text>
                <mj-text align="center">30-day return policy</mj-text>
            </mj-column>
            <mj-column width="33.333%">
                <mj-image src="https://example.com/feature3.png"></mj-image>
                <mj-text align="center" font-weight="700">24/7 Support</mj-text>
                <mj-text align="center">We are here to help</mj-text>
            </mj-column>
        </mj-section>
    </mj-body>
</mjml>
MJML;

$html = $renderer->render($mjml);

Interactive Components

$mjml = <<<'MJML'
<mjml>
    <mj-body>
        <!-- Navigation -->
        <mj-section background-color="#333">
            <mj-column>
                <mj-navbar>
                    <mj-navbar-link href="/" color="#ffffff">Home</mj-navbar-link>
                    <mj-navbar-link href="/products" color="#ffffff">Products</mj-navbar-link>
                    <mj-navbar-link href="/about" color="#ffffff">About</mj-navbar-link>
                </mj-navbar>
            </mj-column>
        </mj-section>

        <!-- Product Carousel -->
        <mj-section>
            <mj-column>
                <mj-text align="center" font-size="24px">Featured Products</mj-text>
                <mj-carousel>
                    <mj-carousel-image src="https://example.com/product1.jpg" />
                    <mj-carousel-image src="https://example.com/product2.jpg" />
                    <mj-carousel-image src="https://example.com/product3.jpg" />
                </mj-carousel>
            </mj-column>
        </mj-section>
    </mj-body>
</mjml>
MJML;

$html = $renderer->render($mjml);

Development

Requirements

  • PHP 8.1 or higher
  • Composer

Installation

git clone https://github.com/dingo-d/php-mjml-renderer.git
cd php-mjml-renderer
composer install

Running Tests

# Run all tests
composer test

# Run only unit tests
composer test:unit

# Run with coverage
composer test:coverage

# Code style check
composer test:style

# Static analysis
composer test:types

Performance

Performance benchmarks on modern hardware:

Test Type Iterations Avg Time Memory
Simple Email 100 0.3ms < 1MB
Complex Email 50 1.2ms < 2MB
Large Email (10 sections) 25 2.8ms < 3MB
Parsing Only 200 0.03ms -
Hero Element 100 0.2ms -
Carousel (5 images) 100 0.2ms -

All benchmarks well within acceptable performance thresholds for production use.

Comparison with Official MJML

This library aims for compatibility with the official MJML specification. While the rendering approach differs (pure PHP vs. Node.js), the output HTML should be functionally equivalent.

Advantages

  • ✅ No Node.js dependency
  • ✅ No external API calls
  • ✅ Pure PHP implementation
  • ✅ Easy integration in PHP projects
  • ✅ Excellent performance
  • ✅ Type-safe with PHP 8.1+

Current Limitations

  • Some advanced head components have limited functionality
  • Custom component plugins not yet supported
  • mj-table and mj-raw have basic implementations

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for detailed guidelines on:

  • Development setup and workflow
  • Coding standards and best practices
  • Testing requirements
  • Pull request process
  • How to implement new MJML elements

Acknowledgments

  • Inspired by the official MJML project
  • Built with modern PHP best practices
  • Community contributions and feedback

License

This project is licensed under the MIT License - see the LICENSE file for details.

dingo-d/php-mjml-renderer 适用场景与选型建议

dingo-d/php-mjml-renderer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 47
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-03