colq2 / blade-mjml
Mjml for Blade.
Fund package maintenance!
colq2
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0||^12.0
- illuminate/filesystem: ^10.0|^11.0|^12.0
- illuminate/view: ^10.0|^11.0|^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- ext-dom: *
- ext-libxml: *
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
- symfony/process: ^7.3
This package is auto-updated.
Last update: 2025-07-10 09:03:04 UTC
README
This package is a port of mjml to laravel blade. The goal is to have a running mjml version purely in php and blade, without the need for node.
You can just use the original mjml xml as blade view.
<!-- resources/views/mail/mjml-example.blade.php -->
<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 World</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
You can now use this view in your mails:
public function content(): Content { return new Content( view: 'mail.mjml-example', ); }
Limitations
-
mj-include is not supported. Use
@include
instead. -
mj-html-attributes not supported yet.
-
mj-raw attribute
position="file-start"
is not supported yet. -
Inline mj-style not working yet.
-
mj-accordion does not handle missing children. You need to always provide mj-accordion-text and mj-accordion-title.
Component overview
Head Components:
- mj-attributes
- mj-breakpoint
- mj-font
- mj-html-attributes
- mj-preview
- mj-style
- mj-title
Body Components:
- mj-accordion
- mj-button
- mj-carousel
- mj-column
- mj-divider
- mj-group
- mj-hero
- mj-image
- mj-navbar
- mj-raw
- mj-section
- mj-social
- mj-spacer
- mj-table
- mj-text
- mj-wrapper
Good to know
The original mjml implementation works differently than the blade compiler. MJML renders recursively, which means that a parent component calls the render function of all its childern and can provide a context or wrap them. The blade compiler works differently. First it compiles the blade view into a php file, then it "just" runs the php file. We cannot provide a context programmatically, because the blade compiler does not know about the context of the parent component.
(Blade do not really know that it is working with html. It does not care, it does not do any html analysis or anything like that. It is just string manipulation.)
This is the biggest challenge of this package. This is why we keep a context stack and a global context.
For example, we need to wrap the child components of a column into a table, which in mjml is done in the mj-column component.
Another challenge was to find out how many siblings a colum has. In the component we don't have any access to the parent html or whatsoever. Now this package "precompiles" the mjml and sets the siblings as an attribute.
The precompilation also changes <mj-> tp <x-mj-> so that the blade compiler can handle it correctly.
Installation
Install the package via composer:
composer require colq2/blade-mjml
Usage
Just put mjml template into your blade files and use the view for sending emails.
Relevant documentation for laravel and mjml:
// resources/views/mail/mjml-example.blade.php
<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 World</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
And use the view in your mail class:
public function content(): Content { return new Content( view: 'mail.mjml-example', ); }
Or render it directly as a view:
Route::get('/mjml-example', function () { return view('mail.mjml-example'); });
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
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.