Answers.Body: <p>There are different ways</p>
  
  <h3>Extending the E-mail template in your template</h3>
  
  <p>This could be your email template <code>emails/_someMail</code></p>
  
  <pre class="lang-twig prettyprint-override"><code>{% extends 'path/of/your/layout-same-as-you-inserted-in-your-cp' %}
  
  {% block main %}Hello World{% endblock %}
  </code></pre>
  
  <p>And in your PHP file</p>
  
  <pre class="lang-PHP prettyprint-override"><code>use craft\mail\Message;
  
  $message = new Message();
  $message-&gt;setTo('john@doe.com');
  $message-&gt;setSubject("Hello world.");
  $message-&gt;setHtmlBody(Craft::$app-&gt;getView()-&gt;renderTemplate('emails/_someMail'));
  $message-&gt;send();
  </code></pre>
  
  <h3>Render the correct template from the beginning</h3>
  
  <p>You can do exactly the same as Craft does in it's function - but I would suggest the first way</p>
  
  <pre class="lang-PHP prettyprint-override"><code>$systemMessage = Craft::$app-&gt;getSystemMessages()-&gt;getMessage('test_email', $message-&gt;language);
  $subjectTemplate = $systemMessage-&gt;subject;
  $textBodyTemplate = $systemMessage-&gt;body;
  
  // Use the site template mode
  $view = Craft::$app-&gt;getView();
  $templateMode = $view-&gt;getTemplateMode();
  $view-&gt;setTemplateMode($view::TEMPLATE_MODE_SITE);
  
  // Use the message language
  $language = Craft::$app-&gt;language;
  if ($message-&gt;language !== null) {
      Craft::$app-&gt;language = $message-&gt;language;
  }
  
  $settings = Craft::$app-&gt;getSystemSettings()-&gt;getEmailSettings();
  $variables = ($message-&gt;variables ?: []) + [
          'emailKey' =&gt; $message-&gt;key,
          'fromEmail' =&gt; $settings-&gt;fromEmail,
          'fromName' =&gt; $settings-&gt;fromName,
      ];
  
  // Render the subject and textBody
  $subject = $view-&gt;renderString($subjectTemplate, $variables);
  $textBody = $view-&gt;renderString($textBodyTemplate, $variables);
  
  // Is there a custom HTML template set?
  if (Craft::$app-&gt;getEdition() === Craft::Pro &amp;&amp; Craft::$app-&gt;getMailer()-&gt;template) {
      $template = Craft::$app-&gt;getMailer()-&gt;template;
  } else {
      // Default to the _special/email.html template
      $view-&gt;setTemplateMode($view::TEMPLATE_MODE_CP);
      $template = '_special/email';
  }
  
  $e = null;
  try {
      $htmlBody = $view-&gt;renderTemplate($template, array_merge($variables, [
          'body' =&gt; Template::raw(Markdown::process($textBody)),
      ]));
  } catch (TemplateLoaderException $e) {
      // Clean up before throwing
  }
  
  // Set things back to normal
  Craft::$app-&gt;language = $language;
  $view-&gt;setTemplateMode($templateMode);
  
  if ($e !== null) {
      throw $e;
  }
  
  $message
      -&gt;setSubject($subject)
      -&gt;setHtmlBody($htmlBody)
      -&gt;setTextBody($textBody);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-19T07:58:11.707Z
Answers.Text: Thank you so much Robin. I always appreciate your relevant answers.
Answers.UserDisplayName: Romain P.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-14T14:34:16.887Z
Answers.Text: SO AWSM - thanks heaps
Answers.UserDisplayName: bhu Boue vidya
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-18T15:44:15.060Z
Answers.LastActivityDate: 2018-09-18T15:54:07.707Z
Answers.LastEditDate: 2018-09-18T15:54:07.707Z
Answers.UserDisplayName: Robin Schambach
Body: <p>On <code>/admin/settings/email</code>, when the <strong>HTML Email Template field</strong> is set to a valid template, all system messages are sent using it. They inject the content of the messages inside the <code>{% block main %}{{body}}{% endblock %}</code> variable.</p>
  
  <p>From a custom plugin, <code>setHtmlBody</code> doesn't inject the content inside it. It takes its value as the whole template value instead of only replacing the <code>{{body}}</code> variable.</p>
  
  <p>Example:</p>
  
  <pre><code>&lt;?php 
  
  use craft\mail\Message;
  
  $message = new Message();
  $message-&gt;setTo('john@doe.com');
  $message-&gt;setSubject("Hello world.");
  $message-&gt;setHtmlBody("Hello world.");
  $message-&gt;send();
  </code></pre>
  
  <p>How can I send this email using the custom HTML email template specified in my CP <strong>Email Settings</strong>?</p>
  
  <p>I could copy-paste the whole file content of my custom template inside the setHtmlBody, but it doesn't seems to be a clean way...</p>
  
Tags: <plugin-development><templating><emails><classes>
Title: How to use the default HTML email template when sending an email from a plugin?
ThreadText: # How to use the default HTML email template when sending an email from a plugin?
  
  (Asked by: username_0 on 2018-09-18)
  
  On `/admin/settings/email`, when the **HTML Email Template field** is set to a valid template, all system messages are sent using it. They inject the content of the messages inside the `{% block main %}{{body}}{% endblock %}` variable.
  
  From a custom plugin, `setHtmlBody` doesn't inject the content inside it. It takes its value as the whole template value instead of only replacing the `{{body}}` variable.
  
  Example:
  
  ```
  <?php 
  
  use craft\mail\Message;
  
  $message = new Message();
  $message->setTo('john@doe.com');
  $message->setSubject("Hello world.");
  $message->setHtmlBody("Hello world.");
  $message->send();
  
  ```
  
  How can I send this email using the custom HTML email template specified in my CP **Email Settings**?
  
  I could copy-paste the whole file content of my custom template inside the setHtmlBody, but it doesn't seems to be a clean way...
  
  **Tags:** plugin-development, templating, emails, classes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-09-18. Score: 7 | Accepted answer
  
  There are different ways
  
  ### Extending the E-mail template in your template
  
  This could be your email template `emails/_someMail`
  
  ```twig
  {% extends 'path/of/your/layout-same-as-you-inserted-in-your-cp' %}
  
  {% block main %}Hello World{% endblock %}
  
  ```
  
  And in your PHP file
  
  ```PHP
  use craft\mail\Message;
  
  $message = new Message();
  $message->setTo('john@doe.com');
  $message->setSubject("Hello world.");
  $message->setHtmlBody(Craft::$app->getView()->renderTemplate('emails/_someMail'));
  $message->send();
  
  ```
  
  ### Render the correct template from the beginning
  
  You can do exactly the same as Craft does in it's function - but I would suggest the first way
  
  ```PHP
  $systemMessage = Craft::$app->getSystemMessages()->getMessage('test_email', $message->language);
  $subjectTemplate = $systemMessage->subject;
  $textBodyTemplate = $systemMessage->body;
  
  // Use the site template mode
  $view = Craft::$app->getView();
  $templateMode = $view->getTemplateMode();
  $view->setTemplateMode($view::TEMPLATE_MODE_SITE);
  
  // Use the message language
  $language = Craft::$app->language;
  if ($message->language !== null) {
      Craft::$app->language = $message->language;
  }
  
  $settings = Craft::$app->getSystemSettings()->getEmailSettings();
  $variables = ($message->variables ?: []) + [
          'emailKey' => $message->key,
          'fromEmail' => $settings->fromEmail,
          'fromName' => $settings->fromName,
      ];
  
  // Render the subject and textBody
  $subject = $view->renderString($subjectTemplate, $variables);
  $textBody = $view->renderString($textBodyTemplate, $variables);
  
  // Is there a custom HTML template set?
  if (Craft::$app->getEdition() === Craft::Pro && Craft::$app->getMailer()->template) {
      $template = Craft::$app->getMailer()->template;
  } else {
      // Default to the _special/email.html template
      $view->setTemplateMode($view::TEMPLATE_MODE_CP);
      $template = '_special/email';
  }
  
  $e = null;
  try {
      $htmlBody = $view->renderTemplate($template, array_merge($variables, [
          'body' => Template::raw(Markdown::process($textBody)),
      ]));
  } catch (TemplateLoaderException $e) {
      // Clean up before throwing
  }
  
  // Set things back to normal
  Craft::$app->language = $language;
  $view->setTemplateMode($templateMode);
  
  if ($e !== null) {
      throw $e;
  }
  
  $message
      ->setSubject($subject)
      ->setHtmlBody($htmlBody)
      ->setTextBody($textBody);
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you so much username_1. I always appreciate your relevant answers.  
  > **username_2**: SO AWSM - thanks heaps  
  
  ---
  
  