Skip to content

Instantly share code, notes, and snippets.

@crabmusket
Last active April 12, 2024 11:39
Show Gist options
  • Select an option

  • Save crabmusket/1f595e679c8f18d45a138d97be23d3e3 to your computer and use it in GitHub Desktop.

Select an option

Save crabmusket/1f595e679c8f18d45a138d97be23d3e3 to your computer and use it in GitHub Desktop.

Revisions

  1. crabmusket revised this gist Aug 19, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MJML.php
    Original file line number Diff line number Diff line change
    @@ -27,14 +27,14 @@ protected function buildView()
    ];
    }

    protected function mjmlToHtml($html)
    protected function mjmlToHtml($mjml)
    {
    $exe = config('mjml.path', base_path('node_modules/.bin/mjml'));
    $command = "$exe --stdin --stdout --config.minify=true";
    $timeout = config('mjml.timeout', 60);

    $process = new Process($command);
    $process->setInput($content);
    $process->setInput($mjml);
    $process->setTimeout($timeout);

    try {
  2. crabmusket revised this gist Aug 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mjml-test.blade.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    <mj-section>
    <mj-column>
    <mj-text>
    Hello {{name}}!
    Hello {{$name}}!
    </mj-text>
    </mj-column>
    </mj-section>
  3. crabmusket revised this gist Aug 6, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions MJMLTestEmail.php
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,6 @@
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Queue\ShouldQueue;

    use App\User;
    use App\Teams\TeamStatistics;
    use App\Mail\MJML;

    class MJMLTestEmail extends Mailable
  4. crabmusket created this gist Aug 6, 2018.
    55 changes: 55 additions & 0 deletions MJML.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    <?php

    namespace App\Mail;

    use Symfony\Component\Process\Process;
    use Symfony\Component\Process\Exception\ProcessFailedException;
    use Symfony\Component\Process\Exception\RuntimeException;
    use Illuminate\Contracts\View\Factory as ViewFactory;
    use Illuminate\Support\HtmlString;

    trait MJML
    {
    /**
    * Provided for semantic clarity, and so that your emails break if you forget
    * to use the trait.
    */
    public function mjml($view, $data = [])
    {
    return $this->view($view, $data);
    }

    protected function buildView()
    {
    $mjml = app(ViewFactory::class)->make($this->view, $this->viewData)->render();
    return [
    'html' => new HTMLString($this->mjmlToHtml($mjml)),
    ];
    }

    protected function mjmlToHtml($html)
    {
    $exe = config('mjml.path', base_path('node_modules/.bin/mjml'));
    $command = "$exe --stdin --stdout --config.minify=true";
    $timeout = config('mjml.timeout', 60);

    $process = new Process($command);
    $process->setInput($content);
    $process->setTimeout($timeout);

    try {
    $process->mustRun();
    } catch (RuntimeException $e) {
    // Process took too long
    throw $e;
    } catch (ProcessFailedException $e) {
    // Process returned an error result
    throw $e;
    } catch (\Exception $e) {
    // Not sure what happened
    throw $e;
    }

    return $process->getOutput();
    }
    }
    27 changes: 27 additions & 0 deletions MJMLTestEmail.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?php

    namespace App\Mail;

    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Queue\ShouldQueue;

    use App\User;
    use App\Teams\TeamStatistics;
    use App\Mail\MJML;

    class MJMLTestEmail extends Mailable
    {
    use Queueable, SerializesModels, MJML;

    public function build()
    {
    return $this
    ->from('you@example.com')
    ->subject('Example')
    ->mjml('emails.mjml-test', [
    'name' => 'Joe Public',
    ]);
    }
    }
    12 changes: 12 additions & 0 deletions mjml-test.blade.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <!-- resources/views/emails/mjml-test.blade.php -->
    <mjml>
    <mj-body>
    <mj-section>
    <mj-column>
    <mj-text>
    Hello {{name}}!
    </mj-text>
    </mj-column>
    </mj-section>
    </mj-body>
    </mjml>