Parse variables into twig email template from custom module I'm trying to send an email from my module and am trying to pass some vars into my module template that I can render using twig. Please note that this is a module template not a front end template.

$first = "john";
$last = "doe";
$mailTo = "john@example.co.uk";
$mailer = Craft::$app-&gt;getMailer();
$mailer-&gt;htmlLayout = false;
$mailer-&gt;textLayout = false;
$mailer-&gt;setViewPath('@modules/businessmodule/templates/emails');
$mailer-&gt;compose('failed_payment.twig', ['first' =&gt; $first, 'last' =&gt; $last])
        -&gt;setTo($mailTo)
        -&gt;setSubject('Failed Payment')
        -&gt;setTextBody('Text fallback')
        -&gt;send();


But I cannot access the vars in my twig template like {{ first }}.

i.e

&lt;p&gt;
    Hi, sorry it didn't work out {{ first }} {{ last }}
&lt;/p&gt;


Am i missing some parse method somewhere?
You can define your module's template root with the following:

Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (craft\events\RegisterTemplateRootsEvent $e) {
    if (is_dir($baseDir = $this-&gt;getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
        $e-&gt;roots[$this-&gt;id] = $baseDir;
    }
});


You then compose emails like so:

$first = "john";
$last = "doe";
$params = ['first' =&gt; $first, 'last' =&gt; $last];
$mailTo = "john@example.co.uk";

$mailer = Craft::$app-&gt;getMailer();
$mailer-&gt;compose()
        -&gt;setTo($mailTo)
        -&gt;setSubject('Failed Payment')
        -&gt;setHtmlBody(
            Craft::$app-&gt;getView()-&gt;renderTemplate('module-id/emails/failed_payment.twig',
            ['user' =&gt; $params])
        )
        -&gt;send();


Replace module-id with your module id as defined in your config/app.php (business-module for you I guess?).

With this in place, you can access the variables in your template by doing:

&lt;p&gt;
    Hi, sorry it didn't work out {{ user.first }} {{ user.last }}
&lt;/p&gt;