I'm trying to integrate MJML with Laravel so we can write super nice email templates. According to this issue, the desired approach for integrating MJML with other template engines is to run the templating engine first, then run MJML on the output once substitutions have been made.
This is fine - I end up with an email template like this, email.blade.php:
<mjml>
<mj-body>
<mj-container background-color="#F9F9F9" width="640px">
<mj-wrapper>
@foreach($sections as $section_name)
<mj-section padding-bottom="0" padding-top="0">
<mj-column>
{{ $section_name }} {{-- for example --}}
</mj-column>
</mj-section>
@endforeach
</mj-wrapper>
</mj-container>
</mj-body>
</mjml>
I've added a custom render() function on my Mailable class that does something like this, running MJML on the command line:
public function render()
{
$result = parent::render(); // this renders the blade file to a string
$process = new Process("mjml --stdin --stdout");
$process->setInput($result);
$process->mustRun();
return $process->getOutput();
}
This works really well if I return the email in a controller for preview in the browser. However, I just discovered that if I actually send the email, render() doesn't get called. It seems that the Mailer actually controls all rendering of email templates for emails that actually get sent out.
So what I'd like to know is - has anyone else done this sort of post-processing before? Is there a relatively easy way I can hook into the Mailer to add my post-processing step? Or do I need to write my own Mailer with a special case for MJML emails and put it in the service container?