Answers.Body: <p>So, you have to "switch" template modes from the look of it! (who knew?!)</p>
  
  <p>This is the code you need to use templates within your module directory, assuming you have your modules template root defined as Oli referenced.</p>
  
  <pre><code>$first = "john";
  $last = "doe";
  $params = ['first' =&gt; $first, 'last' =&gt; $last];
  $mailTo = "john@example.co.uk";
  
  // Switch the template mode
  $oldMode = Craft::$app-&gt;view-&gt;getTemplateMode();
  Craft::$app-&gt;view-&gt;setTemplateMode(View::TEMPLATE_MODE_CP);
  
  $mailer = Craft::$app-&gt;getMailer();
  $mailer-&gt;compose()
          -&gt;setTo($mailTo)
          -&gt;setSubject('Failed Payment')
          -&gt;setHtmlBody(
              Craft::$app-&gt;getView()-&gt;renderTemplate('business-module/emails/failed_payment.twig',
              ['user' =&gt; $params])
          )
          -&gt;send();
  
  // Switch the template mode back
  Craft::$app-&gt;view-&gt;setTemplateMode($oldMode);
  </code></pre>
  
  <p>This took far far too much time to fathom for my liking!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-03T13:40:05.297Z
Answers.LastActivityDate: 2020-04-03T13:40:05.297Z
Answers.UserDisplayName: John
Answers.Body: <p>You can define your module's template root with the following:</p>
  
  <pre class="lang-php prettyprint-override"><code>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;
      }
  });
  </code></pre>
  
  <p>You then compose emails like so:</p>
  
  <pre class="lang-php prettyprint-override"><code>$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();
  </code></pre>
  
  <p>Replace <code>module-id</code> with your module id as defined in your <code>config/app.php</code> (<code>business-module</code> for you I guess?).</p>
  
  <p>With this in place, you can access the variables in your template by doing:</p>
  
  <pre class="lang-twig prettyprint-override"><code>&lt;p&gt;
      Hi, sorry it didn't work out {{ user.first }} {{ user.last }}
  &lt;/p&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-03T13:08:44.717Z
Answers.Text: 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”.
  ```
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-03T15:17:28.153Z
Answers.Text: That's odd... is that a plugin or a module? Anyways, glad you're sorted :)
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-03T15:31:24.740Z
Answers.Text: It's a module!!
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-03T11:01:37.583Z
Answers.LastActivityDate: 2020-04-03T11:01:37.583Z
Answers.UserDisplayName: Oli
Body: <p>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.</p>
  
  <pre><code>$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();
  </code></pre>
  
  <p>But I cannot access the vars in my twig template like {{ first }}.</p>
  
  <p>i.e</p>
  
  <pre><code>&lt;p&gt;
      Hi, sorry it didn't work out {{ first }} {{ last }}
  &lt;/p&gt;
  </code></pre>
  
  <p>Am i missing some parse method somewhere?</p>
  
Tags: <plugin-development><craft3><emails><modules>
Title: Parse variables into twig email template from custom module
ThreadText: # Parse variables into twig email template from custom module
  
  (Asked by: username_0 on 2020-04-02)
  
  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 = "username_0";
  $last = "doe";
  $mailTo = "username_0@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?
  
  **Tags:** plugin-development, craft3, emails, modules
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2020-04-03. Score: 1 | Accepted answer
  
  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 = "username_0";
  $last = "doe";
  $params = ['first' => $first, 'last' => $last];
  $mailTo = "username_0@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!
  
  
  ---
  
  ### Answer by username_1 on 2020-04-03. Score: 1
  
  You can define your module's template root with the following:
  
  ```php
  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:
  
  ```php
  $first = "username_0";
  $last = "doe";
  $params = ['first' => $first, 'last' => $last];
  $mailTo = "username_0@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:
  
  ```twig
  <p>
      Hi, sorry it didn't work out {{ user.first }} {{ user.last }}
  </p>
  
  ```
  
  > **Comments:**
  > **username_0**: 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”. ```  
  > **username_1**: That's odd... is that a plugin or a module? Anyways, glad you're sorted :)  
  > **username_0**: It's a module!!  
  
  ---
  
  