Email template editing in panel?

Is it possible to make the content of plain text email templates editable in the panel (e.g. with a simple textarea)?

a) Keep using email templates and fetch the content somehow?
b) Fetch the template/content in the controller and pass it to the email function?

In both scenarios, would it even be possible to use variables inside the template content?

I only need plain text for now, but I assume using the new blocks it could be cool to create simple html emails through the panel, too.

No idea where to start :smiley:

You could probably do it via MJML. There are PHP libraries like this one that can render it in PHP. Looking at it, you could wire in variables and field data into it, and get a full HTML email at the end of it.

It is possible to set your own blocks up with MJML so you could make ones that line up with the editor blocks and the feed it the block field.

The bonus with MJML is that it renders html that works in all email clients so you dont have to worry about it.

Fetching content from a textarea field into the plain text email template shouldn’t actually be a problem.

The problem I see is using php variables. So you would probably have to use placeholders instead and replace them.

Thanks for confirming this. And your idea using placeholders instead of variables sounds simple and feasible. Thanks

Thanks for reminding me of MJML!

As reference, here’s what my solution looks like:

  • No more email templates in templates/emails.
  • Email templates are now stored as pages, with a dedicated email.yml blueprint
  • The email blueprint has a subject and body field
  • Inside the body I can use placeholders (Mustache)
  • In the controller, instead of passing a template, I now pass the “body” which is the content with replaced placeholders:

Example content:


Here’s the link: {{ link }}


Example controller:

    // Mustache
    $m = new Mustache_Engine;
    // Some variable
    $link = "https://..."

    // Get the content for the email
    $template = kirby()->page("confirmation-email")->body()->value();      

    // Replace any placeholders with actual variables, using Mustache
    $body = $m->render($template, array('link' => $link)); 

        "body" => $body

You don’t need the Mustache engine…

Ha, Kirby’s got ’em all! :smiley:

I’ll stick with Mustache for now as I might have a need for lists/loops and conditional sections.

But good to know there’s a leaner solution for simple plain string replacements :slight_smile: