1

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->getMailer();
$mailer->htmlLayout = false;
$mailer->textLayout = false;
$mailer->setViewPath('@modules/businessmodule/templates/emails');
$mailer->compose('failed_payment.twig', ['first' => $first, 'last' => $last])
        ->setTo($mailTo)
        ->setSubject('Failed Payment')
        ->setTextBody('Text fallback')
        ->send();

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

i.e

<p>
    Hi, sorry it didn't work out {{ first }} {{ last }}
</p>

Am i missing some parse method somewhere?

1

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->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
        $e->roots[$this->id] = $baseDir;
    }
});

You then compose emails like so:

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

$mailer = Craft::$app->getMailer();
$mailer->compose()
        ->setTo($mailTo)
        ->setSubject('Failed Payment')
        ->setHtmlBody(
            Craft::$app->getView()->renderTemplate('module-id/emails/failed_payment.twig',
            ['user' => $params])
        )
        ->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:

<p>
    Hi, sorry it didn't work out {{ user.first }} {{ user.last }}
</p>
| improve this answer | |
  • Hi @oli , thank you for the reply! That isn't able to find the template however and I get this in the logs. ``` 2020-04-03 13:05:53 [-][-][-][trace][craft\web\View::renderTemplate] Rendering template: business-module/emails/failed_payment.twig 2020-04-03 13:05:53 [-][-][-][error][commerce] Exception while processing webhook: Unable to find the template “business-module/emails/failed_payment.twig”. ``` – John Apr 3 at 13:08
  • That's odd... is that a plugin or a module? Anyways, glad you're sorted :) – Oli Apr 3 at 15:17
  • It's a module!! – John Apr 3 at 15:31
1

So, you have to "switch" template modes from the look of it! (who knew?!)

This is the code you need to use templates within your module directory, assuming you have your modules template root defined as Oli referenced.

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

// Switch the template mode
$oldMode = Craft::$app->view->getTemplateMode();
Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);

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

// Switch the template mode back
Craft::$app->view->setTemplateMode($oldMode);

This took far far too much time to fathom for my liking!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.