Skip to content

Core\Mail\Preview::text

$preview->text(): string

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

Every mail has a plain text. A mail program that does not show HTML shows it. It is one of three things:

  • For a string body, it is the body exactly.
  • For a Core\Html\Markup body with the text option, it is the text option exactly.
  • For a Core\Html\Markup body without the option, it is made from the HTML.

When it is made from the HTML, a heading or a paragraph starts a new block after an empty line. A <br> is a line break, and a list item is a line that starts with - . A link is its text and then its URL in brackets. An image is its alt text, and the cells of a table are separated by a tab. <style>, <script> and <head> give no text. A line is wrapped at 78 characters.

The examples below show the plain text of an HTML mail, a plain text written by hand, and a test of a link.

Return value

Returns string. A string body as it is, the text option, or the plain text made from the HTML.

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 plain text of an html mail

<?nvs
// Shows the plain text that is made from the HTML body of a mail.
Core\Mail\Preview $mail = Core\Mail::preview(html`<h1>Your order</h1>
<p>You ordered:</p>
<ul><li>1 x Blue mug</li><li>2 x Tea, 100 g</li></ul>
<p>See the order at <a href="https://example.com/orders/1042">your account</a>.</p>`);
// A heading and a paragraph start a new block. A list item starts with `- `.
// A link is its text, then its URL in brackets.
echo $mail->text();
Output
Your order
You ordered:
- 1 x Blue mug
- 2 x Tea, 100 g
See the order at your account (https://example.com/orders/1042).

Your own plain text

<?nvs
// Shows how the `text` option replaces the plain text that is made from the HTML.
// The HTML has an image. Its plain text is written by hand.
Core\Mail\Preview $mail = Core\Mail::preview(
html`<p><img src="https://example.com/sale.png" alt="Sale"></p>`,
{ text: "Our sale starts today. Every mug is 20% off.\n" },
);
// `text()` returns the `text` option exactly.
echo $mail->text();
Output
Our sale starts today. Every mug is 20% off.

Test the plain text

<?nvs
// Shows how to test that the plain text of a reset mail has the whole reset link.
// proof: test
use Core\Html;
use Core\Html\Markup;
use Core\Str;
use Core\Test;
class Reset
{
// Builds the body of a password reset mail.
public static function body(string $link): Markup
{
return html`<p><a href="` + Html::escape($link) + html`">Choose a new password</a></p>`;
}
}
class ResetTest
{
#[Test]
public function theTextHasTheLink(): void
{
string $link = 'https://example.com/reset?token=7f3a';
Core\Mail\Preview $mail = Core\Mail::preview(Reset::body($link));
// A mail program that shows only text shows this link.
Test::assertTrue(Str::contains($mail->text(), $link));
}
}
Output
ResetTest
✓ theTextHasTheLink
0 failed, 1 passed, 0 skipped, 0 flaky