Skip to content

Core\Mail::preview

Returns the body of a message exactly as Core\Mail::send would send it, and sends nothing.

preview takes the same body as send, and its text and attachments options. It needs no [mail.<name>] block. The result is a Core\Mail\Preview with four methods:

  • html() returns the HTML with its CSS copied into the elements, or null for a string body.
  • text() returns the plain text.
  • keptCss() returns the CSS rules that stay in a <style> element.
  • unusedCss() returns the rules that match no element.

Use it to show a mail on a page, or to check a mail in a test.

Each CSS rule is copied into the style attribute of each element it matches. Most mail programs ignore a <style> element. A style attribute works in almost all of them. A rule such as @media or a:hover stays in the <style> element.

A mistake in the body throws the same RuntimeError as send. For example, a <link rel="stylesheet"> throws, because a mail program does not load a stylesheet.

The examples below show a styled mail, a test of the plain text, and a stylesheet link.

Parameters

  • $bodystring|Core\Html\MarkupThe message, as for send. In a Core\Html\Markup body, each CSS rule of a <style> element is copied into the style attribute of each element it matches. A string from the request is tainted, which means it may contain anything a visitor typed. A tainted string does not compile here, because text() returns it as text that is not tainted.

Options

  • textstringoptionalThe plain-text version of an HTML body, as for send.
  • attachmentsarray<Core\Mail\Attachment>optionalThe attachments, as for send. Each inline image needs its src() in the HTML, and each cid: URL in the HTML needs its inline image.

Return value

Returns Core\Mail\Preview. A Core\Mail\Preview with the HTML and the plain text that send would send, and the CSS rules that stay in a <style> element.

Errors

  • RuntimeErrorThe body or the attachments have a mistake that send throws for. For example, the HTML has a <link rel="stylesheet">. A mail cannot load a stylesheet, so put the CSS in a <style> element.

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

A styled order mail

<?nvs
// Shows how the CSS of a styled mail is copied into the `style` attribute of each element.
// The body has a `<style>` element with two rules.
Core\Mail\Preview $mail = Core\Mail::preview(html`<html><head><style>
h1 { font-size: 20px; color: #222 }
.total { font-weight: bold }
</style></head><body>
<h1>Thank you for your order</h1>
<p class="total">Total: 59.90 EUR</p>
</body></html>`);
// Each rule is now in the `style` attribute of the element it matches.
// No rule is left, so the `<style>` element is removed.
echo $mail->html(), "\n";
Output
<html><head></head><body>
<h1 style="font-size: 20px;color: #222;">Thank you for your order</h1>
<p class="total" style="font-weight: bold;">Total: 59.90 EUR</p>
</body></html>

Check a mail in a test

<?nvs
// Shows how to test the plain text of a welcome mail without sending it.
// proof: test
use Core\Html;
use Core\Html\Markup;
use Core\Test;
class Welcome
{
// Builds the HTML body of the welcome mail. `Html::escape` escapes the name before it is added.
public static function body(string $name): Markup
{
return html`<h1>Welcome</h1><p>Hello ` + Html::escape($name) + html`, your account is ready.</p>`;
}
}
class WelcomeTest
{
#[Test]
public function theTextHasTheName(): void
{
Core\Mail\Preview $mail = Core\Mail::preview(Welcome::body('Ada'));
// The plain text is made from the HTML. An empty line separates the heading and the paragraph.
Test::assertSame($mail->text(), "Welcome\n\nHello Ada, your account is ready.\n");
}
}
Output
WelcomeTest
✓ theTextHasTheName
0 failed, 1 passed, 0 skipped, 0 flaky

A stylesheet link

<?nvs
// Shows what happens when the HTML of a mail links to a stylesheet.
// A mail program does not load a stylesheet, so `preview` throws a `RuntimeError`.
// `send` throws the same error for this body.
try {
Core\Mail::preview(html`<html><head><link rel="stylesheet" href="https://example.com/mail.css"></head><body><p>Hello</p></body></html>`);
echo "previewed\n";
} catch (RuntimeError $error) {
echo $error->message, "\n";
}
Output
Core\Mail::preview: A mail cannot load a stylesheet. Put the CSS in a `<style>` element. `Core\Mail` copies each rule into the elements it matches.