Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
- Support for Laravel 9 - 11 and PHP 8.1 - 8.3
- Use `spatie/mjml-php` package
- Add tests with Pest
  • Loading branch information
fab120 committed Mar 7, 2024
0 parents commit 7889c48
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2

[docker-compose.yml]
indent_size = 4
54 changes: 54 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Run Tests

on: [push, pull_request]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.1, 8.2, 8.3]
laravel: [9.*, 10.*, 11.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: ^7.40
pest: ^1.0
- laravel: 10.*
testbench: ^8.21
pest: ^2.0
- laravel: 11.*
testbench: ^9.0
pest: ^2.0
exclude:
- php: 8.1
laravel: 11.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "pestphp/pest:${{ matrix.pest }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
npm install
- name: Execute tests
run: vendor/bin/pest
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules
/vendor
composer.lock
pnpm-lock.yaml
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to `webnuvola/laravel-mjml` will be documented in this file.

## v1.0.0 - 2024-03-07

- Support for Laravel 9 - 11 and PHP 8.1 - 8.3
- Use `spatie/mjml-php` package
- Add tests with Pest

## Upgrading from v0.4.0 to v1.0.0
Version v1.0.0 is compatible with Mailables defined with version v0.4.0.

This package uses [`spatie/mjml-php`](https://github.com/spatie/mjml-php) under the hood. In your project, or on your server, you must have the JavaScript package [`mjml`](https://github.com/mjmlio/mjml) installed.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Webnuvola

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Laravel MJML
[![Latest Version on Packagist](https://img.shields.io/packagist/v/webnuvola/laravel-mjml.svg?style=flat-square)](https://packagist.org/packages/webnuvola/laravel-mjml)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/webnuvola/laravel-mjml/run-tests.yml?branch=main)](https://github.com/webnuvola/laravel-mjml/actions/workflows/run-tests.yml?query=branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/webnuvola/laravel-mjml.svg?style=flat-square)](https://packagist.org/packages/webnuvola/laravel-mjml)

Effortlessly craft responsive email templates using MJML within Laravel Mailables.

To use this package, follow these steps after generating a new Mailable.
Instead of extending `Illuminate\Mail\Mailable`, extend `Webnuvola\Laravel\Mjml\Mailable`.
In the Mailable class, define the `build` method.
You can now use the `mjml` method for defining a view or `mjmlContent` to directly pass the MJML template.

Here's an example:

```php
use Webnuvola\Laravel\Mjml\Mailable;

class TestMail extends Mailable
{
/**
* Build the message.
*/
public function build(): void
{
return $this->mjml('emails.orders.shipped', [
'order' => $order,
]);
}
}
```

## Installation

You can install the package via composer:

```bash
composer require webnuvola/laravel-mjml
```

In your project, or on your server, you must have the JavaScript package [`mjml`](https://github.com/mjmlio/mjml) installed.

```bash
npm install mjml
```

Make sure you have installed Node 16 or higher.
This package uses [`spatie/mjml-php`](https://github.com/spatie/mjml-php) under the hood.


## Testing

```bash
composer test
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Credits

- [Fabio Cagliero](https://github.com/fab120)

Inspired by [asahasrabuddhe/laravel-mjml](https://github.com/asahasrabuddhe/laravel-mjml) package.

## License

The MIT License (MIT). Please see [License File](LICENSE) for more information.
58 changes: 58 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "webnuvola/laravel-mjml",
"description": "Effortlessly craft responsive email templates using MJML within Laravel Mailables",
"keywords": [
"email",
"laravel",
"mail",
"mailables",
"mjml"
],
"license": "MIT",
"authors": [
{
"name": "Fabio Cagliero",
"email": "fabio@webnuvola.com"
}
],
"require": {
"php": "^8.1",
"html2text/html2text": "^4.3",
"laravel/framework": "^9.0||^10.0||^11.0",
"spatie/mjml-php": "^1.1"
},
"require-dev": {
"orchestra/testbench": "^7.0||^8.0||^9.0",
"pestphp/pest": "^2.34"
},
"autoload": {
"psr-4": {
"Webnuvola\\Laravel\\Mjml\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Webnuvola\\Laravel\\Mjml\\Tests\\": "tests/"
}
},
"scripts": {
"test": "pest"
},
"extra": {
"laravel": {
"providers": [
"Webnuvola\\Laravel\\Mjml\\ServiceProvider"
]
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"mjml": "^4.15.3"
}
}
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
88 changes: 88 additions & 0 deletions src/Mailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Webnuvola\Laravel\Mjml;

use Html2Text\Html2Text;
use Illuminate\Mail\Mailable as IlluminateMailable;
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;
use Spatie\Mjml\Mjml;

/**
* Mailable class.
*
* Inspired from asahasrabuddhe/laravel-mjml
* @see https://github.com/asahasrabuddhe/laravel-mjml
*/
class Mailable extends IlluminateMailable
{
/**
* The MJML template for the message (if applicable).
*/
protected ?string $mjml;

/**
* The MJML content for the message (if applicable).
*/
protected ?string $mjmlContent = null;

/**
* Set the MJML template for the message.
*/
public function mjml(string $view, array $data = []): static
{
$this->mjml = $view;
$this->viewData = array_merge($this->buildViewData(), $data);

return $this;
}

/**
* Set the MJML content for the message.
*/
public function mjmlContent(string $mjmlContent): static
{
$this->mjmlContent = $mjmlContent;

return $this;
}

/**
* Build the view for the message.
*
* @return array|string
*
* @throws \ReflectionException
*/
protected function buildView()
{
if (isset($this->mjml) || isset($this->mjmlContent)) {
return $this->buildMjmlView();
}

return parent::buildView();
}

/**
* Build the MJML view for the message.
*/
protected function buildMjmlView(): array
{
if (isset($this->mjml)) {
$this->mjmlContent = View::make($this->mjml, $this->buildViewData());
}

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

return [
'html' => new HtmlString($html),
'text' => new HtmlString(
html_entity_decode(
preg_replace("/[\r\n]{2,}/", "\n\n", (new Html2Text($html, ['width' => 0]))->getText()),
ENT_QUOTES,
'UTF-8',
),
),
];
}
}
Loading

0 comments on commit 7889c48

Please sign in to comment.