Skip to content

Core\Mail\Preview::html

$preview->html(): ?Core\Html\Markup

html() returns the HTML that Core\Mail::send would send for the body of a Core\Mail\Preview. You call html() on the result of Core\Mail::preview.

Most mail programs ignore a <style> element. So each CSS rule of a <style> element is copied into the style attribute of each element it matches. When two rules set the same property, the more specific rule wins. When both are equally specific, the later rule wins. A value with !important wins over a normal value. The element's own style attribute wins over a normal rule.

A rule that is copied is removed from the <style> element. An empty <style> element is removed. A rule that cannot be copied stays, such as @media or a:hover. keptCss() lists those rules.

The result is a whole HTML document. When the body has no <html>, <head> or <body> element, html() adds it.

When the body is a string, the mail has no HTML, and html() returns null. Use text() for the plain text.

The examples below show a button that gets its style, an element whose own style attribute wins, and a plain-text mail with no HTML.

Return value

Returns ?Core\Html\Markup. The HTML, or null when the body is a string.

Changelog

VersionChange
0.0.6Core\Mail::preview builds a mail body as send would and sends nothing. It returns the HTML with its CSS inlined, the plain text, and the CSS rules that stayed in <style>.

Examples

The button gets its style

<?nvs
// Shows the HTML of a mail after its CSS is copied into the `style` attribute of each element.
Core\Mail\Preview $mail = Core\Mail::preview(html`<style>
.button { background: #0a5; color: white; padding: 12px }
p { margin: 0 }
</style>
<p>Your password was changed.</p>
<p><a class="button" href="https://example.com/account">Open your account</a></p>`);
// Both paragraphs get the `p` rule. The link gets the `.button` rule.
echo $mail->html(), "\n";
Output
<html><head>
</head><body><p style="margin: 0;">Your password was changed.</p>
<p style="margin: 0;"><a class="button" href="https://example.com/account" style="background: #0a5;color: white;padding: 12px;">Open your account</a></p></body></html>

A style attribute wins

<?nvs
// Shows which value an element gets when a CSS rule and its own `style` attribute set the same property.
Core\Mail\Preview $mail = Core\Mail::preview(html`<style>
p { color: #333; font-size: 14px }
.warning { color: red !important }
</style>
<p style="color: blue">A normal notice.</p>
<p class="warning" style="color: blue">Your card expires soon.</p>`);
// The first paragraph keeps `color: blue` from its `style` attribute.
// In the second one, `!important` in the `.warning` rule sets the color to red.
echo $mail->html(), "\n";
Output
<html><head>
</head><body><p style="font-size: 14px;color: blue">A normal notice.</p>
<p class="warning" style="font-size: 14px;color: red !important">Your card expires soon.</p></body></html>

A plain text mail has no html

<?nvs
// Shows that `html()` returns `null` for a mail with a `string` body.
// A `string` body is sent as plain text, so the mail has no HTML.
Core\Mail\Preview $mail = Core\Mail::preview('Your code is 482913.');
// `html()` returns a `?Core\Html\Markup`. `??` gives the text after it when the result is `null`.
echo $mail->html() ?? 'This mail has no HTML.', "\n";
echo $mail->text(), "\n";
Output
This mail has no HTML.
Your code is 482913.