<p>Not exactly sure since when this is possible, but there is a way nicer way of doing this now:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php
$mailer = Craft::$app-&gt;getMailer();
$mailer-&gt;htmlLayout = false;
$mailer-&gt;textLayout = false;
$mailer-&gt;setViewPath('@templates/mailer');
$mailer-&gt;compose('_template_name.twig', ['user' =&gt; $user])
    -&gt;setTo('example@example.com')
    -&gt;setSubject('This is the subject')
    -&gt;setTextBody('Text fallback')
    -&gt;send();
</code></pre>

CC BY-SA 4.0
2020-02-21T08:04:25.140Z
2020-02-21T08:04:25.140Z
Sascha Gehlich
<p>I think you have to set the template mode to 'SITE', to let Craft know where to look for the template:</p>

<pre><code>$view = \Craft::$app-&gt;getView();         
if ($view-&gt;getTemplateMode() !== $view::TEMPLATE_MODE_SITE) {
    $view-&gt;setTemplateMode($view::TEMPLATE_MODE_SITE);
}

Craft::$app-&gt;mailer-&gt;compose('_email.html', ['body' =&gt; $emailBody])
    -&gt;setFrom($fromMail)
    -&gt;setTo($toMail)
    -&gt;setSubject($subject)
    -&gt;send();
</code></pre>

CC BY-SA 4.0
2018-11-08T11:51:12.230Z
2018-11-08T11:51:12.230Z
Paul
<p>I've been struggling to make this work within a plugin. I have a html file in my templates folder called _email.html. I try to load it in my plugin but then I get an error. This is my code:</p>

<pre><code>Craft::$app-&gt;mailer-&gt;compose('_email.html', ['body' =&gt; $emailBody])
            -&gt;setFrom($fromMail)
            -&gt;setTo($toMail)
            -&gt;setSubject($subject)
            -&gt;send();
</code></pre>

<p>The problem is that the Yii mailer is looking in the vendor folder instead of the templates folder.</p>

<p>This is the error I get:</p>

<p><code>The view file does not exist: /Applications/MAMP/htdocs/project/vendor/craftcms/cms/src/mail/_email</code></p>

<p>Even when I try going back to the templates folder it doesn't seem to work and I get the following error:</p>

<p><code>The view file does not exist: /Applications/MAMP/htdocs/project/vendor/craftcms/cms/src/mail/../../../../../../templates/_email</code></p>

<p>I found out that if I copy and paste the contents of the _email.html file and do the following it does work, but this obviously is a pretty bad workaround</p>

<pre><code>-&gt;setHtmlBody("&lt;!DOCTYPE html......")
</code></pre>

<p>Any advice?</p>

<p>Thanks!</p>

<plugin-development><craft3><emails>
Loading email templates in a Craft 3 plugin
# Loading email templates in a Craft 3 plugin

(Asked by: username_0 on 2018-11-08)

I've been struggling to make this work within a plugin. I have a html file in my templates folder called _email.html. I try to load it in my plugin but then I get an error. This is my code:
Craft::$app->mailer->compose('_email.html', ['body' => $emailBody])
            ->setFrom($fromMail)
            ->setTo($toMail)
            ->setSubject($subject)
            ->send();

The problem is that the Yii mailer is looking in the vendor folder instead of the templates folder.
This is the error I get:
The view file does not exist: /Applications/MAMP/htdocs/project/vendor/craftcms/cms/src/mail/_email
Even when I try going back to the templates folder it doesn't seem to work and I get the following error:
The view file does not exist: /Applications/MAMP/htdocs/project/vendor/craftcms/cms/src/mail/../../../../../../templates/_email
I found out that if I copy and paste the contents of the _email.html file and do the following it does work, but this obviously is a pretty bad workaround
->setHtmlBody("
Any advice?
Thanks!

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


---

## 2 answers

### Answer by username_1 on 2020-02-21. Score: 1 | Accepted answer

Not exactly sure since when this is possible, but there is a way nicer way of doing this now:
getMailer();
$mailer->htmlLayout = false;
$mailer->textLayout = false;
$mailer->setViewPath('@templates/mailer');
$mailer->compose('_template_name.twig', ['user' => $user])
    ->setTo('example@example.com')
    ->setSubject('This is the subject')
    ->setTextBody('Text fallback')
    ->send();


---

### Answer by username_2 on 2018-11-08. Score: 2

I think you have to set the template mode to 'SITE', to let Craft know where to look for the template:
$view = \Craft::$app->getView();         
if ($view->getTemplateMode() !== $view::TEMPLATE_MODE_SITE) {
    $view->setTemplateMode($view::TEMPLATE_MODE_SITE);
}

Craft::$app->mailer->compose('_email.html', ['body' => $emailBody])
    ->setFrom($fromMail)
    ->setTo($toMail)
    ->setSubject($subject)
    ->send();


---

