Answers.Body: <p>You probably just need to call <a href="http://buildwithcraft.com/classreference/services/PathService#setTemplatesPath-detail" rel="nofollow">PathService::setTemplatesPath()</a> and pass in the path to your plugin's template folder.</p>
  
  <p>It would look something like:</p>
  
  <pre><code>// Save the current template path.
  $originalPath = craft()-&gt;path-&gt;getTemplatesPath();
  
  // Set your plugin's template path.
  $path = craft()-&gt;path-&gt;getPluginsPath().'/mypluginfolder/templates/';
  craft()-&gt;path-&gt;setTemplatesPath($path);
  
  //Send to each User
  foreach ($users as $user)
  {
      //Send Mail
      $email = new EmailModel();
      $email-&gt;toEmail = $user-&gt;email;
      $email-&gt;subject = 'New Question';
      $email-&gt;htmlBody = "{% extends '{$template}' %}".
          "{% set body %}".
          '
          &lt;p&gt;A new question has been posted by '.$entry-&gt;author-&gt;name.':&lt;/p&gt;
  
          &lt;h2&gt;Question: '.$entry-&gt;title.'&lt;/h2&gt;
  
          &lt;p&gt;Description: '.$entry-&gt;questionDetails.'&lt;/p&gt;
  
          &lt;p&gt;Attachments: '.$entry-&gt;questionAttachments-&gt;total().'&lt;/p&gt;
  
          &lt;p&gt;&lt;a style="color: #09c;" href="'.$entry-&gt;url.'" title="'.$entry-&gt;title.'"&gt;&lt;b&gt;Answer Question&lt;/b&gt;&lt;/a&gt;&lt;/p&gt;
                      '
          ."{% endset %}";
  
      craft()-&gt;email-&gt;sendEmail($email);
  }
  
  // Reset the original path.
  craft()-&gt;path-&gt;setTemplatesPath($originalPath);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-14T04:17:57.373Z
Answers.LastActivityDate: 2015-09-14T04:17:57.373Z
Answers.UserDisplayName: Brad Bell
Body: <p>I've written a plugin that sends and email to a user group on new entry save in a particular section. Most entries will be made from the front end, and the plugin is working great in that regard. However, if a user attempts to create a new entry from the CP, the plugin cannot find my designated template path and throws an error. Any help is greatly appreciated. </p>
  
  <pre><code>if ($isNewEntry and $entrySection == 'Questions')
  {
      //Get all users from UserGroup
      $user_criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
      $user_criteria-&gt;groupId = '9';
      $users = $user_criteria-&gt;find();
      $template = '/email/question-email.html';
  
      //Send to each User
      foreach ($users as $user)
      {
          //Send Mail
          $email = new EmailModel();
          $email-&gt;toEmail = $user-&gt;email;
          $email-&gt;subject = 'New Question';
          $email-&gt;htmlBody = "{% extends '{$template}' %}".
              "{% set body %}".
              '
              &lt;p&gt;A new question has been posted by '.$entry-&gt;author-&gt;name.':&lt;/p&gt;
  
              &lt;h2&gt;Question: '.$entry-&gt;title.'&lt;/h2&gt;
  
              &lt;p&gt;Description: '.$entry-&gt;questionDetails.'&lt;/p&gt;
  
              &lt;p&gt;Attachments: '.$entry-&gt;questionAttachments-&gt;total().'&lt;/p&gt;
  
              &lt;p&gt;&lt;a style="color: #09c;" href="'.$entry-&gt;url.'" title="'.$entry-&gt;title.'"&gt;&lt;b&gt;Answer Question&lt;/b&gt;&lt;/a&gt;&lt;/p&gt;
                          '
              ."{% endset %}";
  
          craft()-&gt;email-&gt;sendEmail($email);
      }
  }
  </code></pre>
  
Tags: <plugin-development><emails>
Title: Template path from plugin not working from CP
ThreadText: # Template path from plugin not working from CP
  
  (Asked by: username_0 on 2015-09-13)
  
  I've written a plugin that sends and email to a user group on new entry save in a particular section. Most entries will be made from the front end, and the plugin is working great in that regard. However, if a user attempts to create a new entry from the CP, the plugin cannot find my designated template path and throws an error. Any help is greatly appreciated.
  
  ```
  if ($isNewEntry and $entrySection == 'Questions')
  {
      //Get all users from UserGroup
      $user_criteria = craft()->elements->getCriteria(ElementType::User);
      $user_criteria->groupId = '9';
      $users = $user_criteria->find();
      $template = '/email/question-email.html';
  
      //Send to each User
      foreach ($users as $user)
      {
          //Send Mail
          $email = new EmailModel();
          $email->toEmail = $user->email;
          $email->subject = 'New Question';
          $email->htmlBody = "{% extends '{$template}' %}".
              "{% set body %}".
              '
              <p>A new question has been posted by '.$entry->author->name.':</p>
  
              <h2>Question: '.$entry->title.'</h2>
  
              <p>Description: '.$entry->questionDetails.'</p>
  
              <p>Attachments: '.$entry->questionAttachments->total().'</p>
  
              <p><a style="color: #09c;" href="'.$entry->url.'" title="'.$entry->title.'"><b>Answer Question</b></a></p>
                          '
              ."{% endset %}";
  
          craft()->email->sendEmail($email);
      }
  }
  
  ```
  
  **Tags:** plugin-development, emails
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-09-14. Score: 1 | Accepted answer
  
  You probably just need to call [PathService::setTemplatesPath()](http://buildwithcraft.com/classreference/services/PathService#setTemplatesPath-detail) and pass in the path to your plugin's template folder.
  
  It would look something like:
  
  ```
  // Save the current template path.
  $originalPath = craft()->path->getTemplatesPath();
  
  // Set your plugin's template path.
  $path = craft()->path->getPluginsPath().'/mypluginfolder/templates/';
  craft()->path->setTemplatesPath($path);
  
  //Send to each User
  foreach ($users as $user)
  {
      //Send Mail
      $email = new EmailModel();
      $email->toEmail = $user->email;
      $email->subject = 'New Question';
      $email->htmlBody = "{% extends '{$template}' %}".
          "{% set body %}".
          '
          <p>A new question has been posted by '.$entry->author->name.':</p>
  
          <h2>Question: '.$entry->title.'</h2>
  
          <p>Description: '.$entry->questionDetails.'</p>
  
          <p>Attachments: '.$entry->questionAttachments->total().'</p>
  
          <p><a style="color: #09c;" href="'.$entry->url.'" title="'.$entry->title.'"><b>Answer Question</b></a></p>
                      '
          ."{% endset %}";
  
      craft()->email->sendEmail($email);
  }
  
  // Reset the original path.
  craft()->path->setTemplatesPath($originalPath);
  
  ```
  
  
  ---
  
  