Answers.Body: <p>Thanks to Brad for providing the bulk of the answer. In more detail the line needing to be changed was this:</p>
  
  <pre><code>$notificationEmail -&gt; htmlBody = craft()-&gt;templates-&gt;render('PLUGIN-FOLDER-NAME/TEMPLATE-NAME', ['theUser' =&gt; $user]);
  </code></pre>
  
  <p>I could then use {{ theUser }} (Which is a UserModel) in the TEMPLATE-NAME template to generate everything I needed.</p>
  
  <p>So the full solution, to send an Email from a plugin, but using a Twig template to generate the HTML email content was:</p>
  
  <pre><code>$myEmail             = new EmailModel();
  $myEmail -&gt; toEmail  = $user-&gt;email;
  $myEmail -&gt; subject  = "Subject here";
  $myEmail -&gt; body     = "PLAIN TEXT CONTENT";
  $myEmail -&gt; htmlBody = craft()-&gt;templates-&gt;render('PLUGIN-FOLDER-NAME/TEMPLATE-NAME', ['theUser' =&gt; $user]);
  craft()-&gt;email-&gt;sendEmail($myEmail);
  </code></pre>
  
  <p>Where $user was a variable containing a UserModel which I'd already got from previous code in the plugin I was writing.</p>
  
  <p><a href="https://craftcms.com/classreference/services/TemplatesService#render-detail" rel="nofollow noreferrer">https://craftcms.com/classreference/services/TemplatesService#render-detail</a> for more detail (but for which I wish there were example code of how to use, and not just a dictionary).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-01T12:13:45.583Z
Answers.LastActivityDate: 2017-11-01T12:13:45.583Z
Answers.UserDisplayName: Matt Wilcox
Body: <p>I'm trying to do a nicely formatted email from a plugin I'm making. The standard way I've been doing simple emails is all entirely in the code, somewhat like this:</p>
  
  <pre><code>$notificationEmail = new EmailModel();
  $notificationEmail -&gt; toEmail  = $userEmail;
  $notificationEmail -&gt; subject  = "Subject here";
  $notificationEmail -&gt; body     = "Thanks $userName, you’re now registered on our website.";
  $notificationEmail -&gt; htmlBody = " ... MASSIVE COMPLICATED STUFF THATS FAR BETTER SUITED TO TWIG ... "
  craft()-&gt;email-&gt;sendEmail($notificationEmail);
  </code></pre>
  
  <p>I've now got an email that is far better suited to being generated via a Twig template because I'm going to have to loop through relational fields and all sorts. I have no idea how and I don't seem to know what to Google to get what I'm after... Anyone got pointers to this sort of thing?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-31T17:15:57.257Z
Comments.Text: Guessing either `$notificationEmail->htmlBody = craft()->templates->renderString($myComplicatedStuff);` or `$notificationEmail->htmlBody = craft()->templates->renderTemplate('/path/to/my/complicated/template');` is what you're looking for?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-31T17:23:05.487Z
Comments.Text: Quite possibly the latter! How do I pass the User or other variables to that template though?
Comments.UserDisplayName: Matt Wilcox
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-31T17:28:17.367Z
Comments.Text: Actually it's just `render()` for the latter and takes a 2nd $variables param. https://craftcms.com/classreference/services/TemplatesService#render-detail
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-31T17:28:38.287Z
Comments.Text: Let me now if that works for you and I'll add it as an answer.
Comments.UserDisplayName: Brad Bell
Tags: <plugin-development><emails>
Title: How do I use a Twig template to generate Email content from inside a plugin I'm making?
ThreadText: # How do I use a Twig template to generate Email content from inside a plugin I'm making?
  
  (Asked by: username_0 on 2017-10-31)
  
  I'm trying to do a nicely formatted email from a plugin I'm making. The standard way I've been doing simple emails is all entirely in the code, somewhat like this:
  
  ```
  $notificationEmail = new EmailModel();
  $notificationEmail -> toEmail  = $userEmail;
  $notificationEmail -> subject  = "Subject here";
  $notificationEmail -> body     = "Thanks $userName, you’re now registered on our website.";
  $notificationEmail -> htmlBody = " ... MASSIVE COMPLICATED STUFF THATS FAR BETTER SUITED TO TWIG ... "
  craft()->email->sendEmail($notificationEmail);
  
  ```
  
  I've now got an email that is far better suited to being generated via a Twig template because I'm going to have to loop through relational fields and all sorts. I have no idea how and I don't seem to know what to Google to get what I'm after... Anyone got pointers to this sort of thing?
  
  **Tags:** plugin-development, emails
  
  > **Comments:**
  > **username_1**: Guessing either `$notificationEmail->htmlBody = craft()->templates->renderString($myComplicatedStuff);` or `$notificationEmail->htmlBody = craft()->templates->renderTemplate('/path/to/my/complicated/template');` is what you're looking for?  
  > **username_0**: Quite possibly the latter! How do I pass the User or other variables to that template though?  
  > **username_1**: Actually it's just `render()` for the latter and takes a 2nd $variables param. https://craftcms.com/classreference/services/TemplatesService#render-detail  
  > **username_1**: Let me now if that works for you and I'll add it as an answer.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-11-01. Score: 0 | Accepted answer
  
  Thanks to username_1 for providing the bulk of the answer. In more detail the line needing to be changed was this:
  
  ```
  $notificationEmail -> htmlBody = craft()->templates->render('PLUGIN-FOLDER-NAME/TEMPLATE-NAME', ['theUser' => $user]);
  
  ```
  
  I could then use {{ theUser }} (Which is a UserModel) in the TEMPLATE-NAME template to generate everything I needed.
  
  So the full solution, to send an Email from a plugin, but using a Twig template to generate the HTML email content was:
  
  ```
  $myEmail             = new EmailModel();
  $myEmail -> toEmail  = $user->email;
  $myEmail -> subject  = "Subject here";
  $myEmail -> body     = "PLAIN TEXT CONTENT";
  $myEmail -> htmlBody = craft()->templates->render('PLUGIN-FOLDER-NAME/TEMPLATE-NAME', ['theUser' => $user]);
  craft()->email->sendEmail($myEmail);
  
  ```
  
  Where $user was a variable containing a UserModel which I'd already got from previous code in the plugin I was writing.
  
  <https://craftcms.com/classreference/services/TemplatesService#render-detail> for more detail (but for which I wish there were example code of how to use, and not just a dictionary).
  
  
  ---
  
  