Loading email templates in a Craft 3 plugin 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-&gt;mailer-&gt;compose('_email.html', ['body' =&gt; $emailBody])
            -&gt;setFrom($fromMail)
            -&gt;setTo($toMail)
            -&gt;setSubject($subject)
            -&gt;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

-&gt;setHtmlBody("&lt;!DOCTYPE html......")


Any advice?

Thanks!
I think you have to set the template mode to 'SITE', to let Craft know where to look for the template:

$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();