<p>For anyone struggling with this, apparently this is one way of doing it.</p>

<p>I struggled with this for quite some time before posting my original question, but regardless, sorry about that.</p>

<p>Anyway, here it is.</p>

<pre><code>Craft::$app-&gt;setViewPath('/path/to/templates/');
$body = Craft::$app-&gt;getView()-&gt;renderTemplate('confirmation.twig', ['orderId' =&gt; $orderId, 'customerId' =&gt; $customerId]);
Craft::$app-&gt;mailer-&gt;compose()
    -&gt;setFrom(['me@me.com')
    -&gt;setTo('me@me.com')
    -&gt;setSubject('hey')
    -&gt;setHtmlBody($body)
    -&gt;send();
</code></pre>

<p>Edit:</p>

<p>There's probably a better way of doing this, but if you're working in multiple environments, you can do this in <code>general.php</code></p>

<pre><code>'dev' =&gt; [
    'templatePath' =&gt; '/Users/stian/dev/site.com/templates/',
],
'staging' =&gt; [
    'templatePath' =&gt; '/Users/stian/dev/site.com/templates/',
],
etc
</code></pre>

<p>and then <code>Craft::$app-&gt;setViewPath(Craft::$app-&gt;config-&gt;general-&gt;templatePath)</code> in your plugin/module.</p>

<p>PS! Will mark as answered once I'm allowed</p>

CC BY-SA 4.0
2019-01-20T17:08:53.177Z
2019-01-20T17:50:28.800Z
2019-01-20T17:50:28.800Z
Stian Karlsen
<p>Trying to send an email</p>

<pre><code>Craft::$app-&gt;setViewPath('/Users/stian/dev/site.com/templates/');
Craft::$app-&gt;mailer-&gt;compose('confirmation.twig', ['orderId' =&gt; $orderId, 'customerId' =&gt; $customerId])
    -&gt;setFrom('me@me.com')
    -&gt;setTo('me@me.com')
    -&gt;setSubject('hey')
    -&gt;send();
</code></pre>

<p>Getting <code>The view file does not exist: /Users/stian/dev/site.com/vendor/craftcms/cms/src/mail/confirmation.twig</code></p>

<p>Assuming I correctly understand how to render a view for sending as an email, how can I correctly point to its location?</p>

<plugin-development><craft3><emails>
How to set view path for rendering email templates?
# How to set view path for rendering email templates?

(Asked by: username_0 on 2019-01-20)

Trying to send an email
Craft::$app->setViewPath('/Users/stian/dev/site.com/templates/');
Craft::$app->mailer->compose('confirmation.twig', ['orderId' => $orderId, 'customerId' => $customerId])
    ->setFrom('me@me.com')
    ->setTo('me@me.com')
    ->setSubject('hey')
    ->send();

Getting The view file does not exist: /Users/stian/dev/site.com/vendor/craftcms/cms/src/mail/confirmation.twig
Assuming I correctly understand how to render a view for sending as an email, how can I correctly point to its location?

**Tags:** plugin-development, craft3, emails


---

## 1 answers

### Answer by username_0 on 2019-01-20. Score: 3 | Accepted answer

For anyone struggling with this, apparently this is one way of doing it.
I struggled with this for quite some time before posting my original question, but regardless, sorry about that.
Anyway, here it is.
Craft::$app->setViewPath('/path/to/templates/');
$body = Craft::$app->getView()->renderTemplate('confirmation.twig', ['orderId' => $orderId, 'customerId' => $customerId]);
Craft::$app->mailer->compose()
    ->setFrom(['me@me.com')
    ->setTo('me@me.com')
    ->setSubject('hey')
    ->setHtmlBody($body)
    ->send();

Edit:
There's probably a better way of doing this, but if you're working in multiple environments, you can do this in general.php
'dev' => [
    'templatePath' => '/Users/stian/dev/site.com/templates/',
],
'staging' => [
    'templatePath' => '/Users/stian/dev/site.com/templates/',
],
etc

and then Craft::$app->setViewPath(Craft::$app->config->general->templatePath) in your plugin/module.
PS! Will mark as answered once I'm allowed


---

