Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Render an email template programatically

2
by BennoThommo, last modified on August 6th, 2021

Inspired by the following October trick from OFFLINE: Render an e-mail template in the browser

The following code allows you to programatically render an email template. You can place this within your controller, or as the response to a route, or as a variable for a page or layout template.

// Within your use cases
use System\Classes\MailManager;
use System\Models\MailLayout;
use System\Models\MailTemplate;

// The code
$data = [
    'key' => 'value',
    'foo' => 'bar',
];

$layout = new MailLayout;
$layout->fillFromCode('default'); // Change this to use another layout.

$template = new MailTemplate;
$template->layout = $layout;
$template->fillFromView('winter.plugin::mail.templates.your-template'); // Use the mail template code here.

$mailContent = MailManager::instance()->renderTemplate($template, $data);

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?