How to use the default HTML email template when sending an email from a plugin? 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:

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


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...
There are different ways

Extending the E-mail template in your template

This could be your email template emails/_someMail

{% extends 'path/of/your/layout-same-as-you-inserted-in-your-cp' %}

{% block main %}Hello World{% endblock %}


And in your PHP file

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


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

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