escuelademusica/laravel-mjml

0.1.2 2022-03-14 07:07 UTC

This package is auto-updated.

Last update: 2024-04-14 11:40:26 UTC


README

Installation

composer require escuelademusica/laravel-mjml

You need install MJML via npm.

npm install --save mjml

You can customize the location of the MJML files by adding the following to your .env file: MJML_BINARY_PATH=/path/to/mjml/bin/mjml

Usage

There are two ways to use this package.

1. Extends our Custom Mailable

<?php

namespace App\Mail;

use EscuelaDeMusica\MJML\Mail\Mailable;

class CustomMailable extends Mailable
{

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->mjml('view.name',);
    }
}

2. Use the InteractsWithMjml Trait with laravel mailable

This works the same way as the CustomMailable, but you don't need to extend the mailable. You can use the trait with any laravel mailable, but remember to edit the render function to look like this:

<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;
use EscuelaDeMusica\MJML\InteractsWithMjml;

class SomeEmail extends Mailable
{
    use InteractsWithMjml;

    public function buildView()
    {
        if (isset($this->mjml)) {
            return $this->buildMjmlView();
        }

        return parent::buildView();
    }

    public function build ()
    {
        return $this->mjml('view.name');
    }

    public function render()
    {
        return $this->renderMjml();
    }
}

For laravel notifications in your toMail method, you will normally return an instance of a MailMessage. The package extends that class and adds the mjml functions to it. To use mjml mails for notifications, you will need to extend the MjmlMessage provided by the package.

<?php

namespace App\Notifications;

use EscuelaDeMusica\MJML\Mail\Messages\MjmlMessage;

class SomeNotification extends Notification
{
    public function toMail($notifiable)
    {
        return (new MjmlMessage)
            ->subject('Notification Subject')
            ->mjml('notification.name');
    }
}

How it works.

To include other files in your mjml, you can use the mj_include directive. The directive takes the path to the file, and the mjml-include type.

@mjml('view.name');

This is inspired by (https://github.com/asahasrabuddhe/laravel-mjml), but more optimized. Compiles the mjml of view compiled file.

Also