superbigco / craft-mjml

Render Twig emails with MJML.
https://superbig.co
MIT License
25 stars 5 forks source link

`MJMLService::include` unconfigurable template mode #21

Closed soulseekah closed 5 months ago

soulseekah commented 9 months ago

Craft core Mailer assumes all email templates are View::TEMPLATE_MODE_CP which grabs templates from /app/templates/ without the additional in siteHandle for the current or default site. This is configurable. But the MJMLService::include method looks in View::TEMPLATE_MODE_SITE instead, which grabs templates from /app/templates/$siteHandle/mjml. This means that we have to copy and maintain MJML templates for all sites. There is hardly a good way to override this behavior.

This can be reproduced when triggering an email from the CP. Also via unit tests.

$message = (new Message())
            ->setHtmlBody(
                (string) Template::raw(
                    MJML::$plugin->mjmlService->include(
                        'mjml/welcome.mjml'
                    )
                )
            )
            ->setTo($this->to)
            ->setSubject($entry->title);

        return $this->mailer->send($message);

Error: [mjml] 'Could not generate output: Could not render template: mjml/subsite/_mjml-welcome.twig'

sjelfull commented 5 months ago

Thanks for reporting this!

After digging into this, I noticed the same thing: that the core mailer will set the template mode to use the CP templates, even though we do override the template mode in the internal plugin parser.

I have a fix for this that will be included in the next version. It will make sure all include paths will be relative to your site templates folder.

I don't think your full description is correct, however: it is not necessary to copy all your MJML templates to every site subfolder - Craft will look into both your main templates folder AND your site subfolder, and pick the most specific one. This I conclude both from reading the core code and from testing on a multisite setup.

Let me know if the fix doesn't fully resolve your issue.