Core\Test\SentMail::html
$sentMail->html(): ?stringhtml() returns the HTML body of a message. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and html() is one of its methods.
Your program sends HTML when the body it gives to Core\Mail::send is a Core\Html\Markup. html() returns the HTML that was sent, with the CSS of each <style> element already copied into the elements. When the body was a string, html() returns null. The plain-text body is always there. Use text() to read it.
Use html() to check what a person sees in a mail program that shows HTML. For example, a test can check that a name from a form is escaped, so that it shows as text and not as tags.
The examples below show a name that is escaped in the HTML body, a short notice that has no HTML body, and a request that sends a mail with the same link in both bodies.
Return value
Returns ?string. The HTML that is sent, or null when the body was a string.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Test::sentMail returns the messages your program sent in the current test. Each one is a Core\Test\SentMail, and its methods return the values given to Core\Mail::send. |
0.0.6 | Core\Test\SentMail::html returns the HTML as sent, with its CSS inlined. text returns the plain text as sent, which Novis makes from the HTML when the program gives none. |
Examples
The examples below run with this configuration file.
# The mail server for the examples. Nothing listens on port 1, so a real send# would fail. Under `nvs test`, no message is sent and no connection is opened.[mail.site]host = "127.0.0.1"port = 1from = "hello@example.com"timeout = "1s"
# The program may send mail through the `site` block.[capabilities.mail]send = ["site"]The name is escaped
<?nvs// Shows how to test that a name in the HTML body of a mail is escaped.// proof: test
use Core\Html;use Core\Html\Markup;use Core\Mail;use Core\Test;
class Invite{ // Sends an invitation. The name goes into the HTML body through `Html::escape`. public static function send(string $email, string $name): void { Markup $body = ('<p>Hello ' as Markup) + Html::escape($name) + ('</p>' as Markup); Mail::send('site', [$email], 'You are invited', $body); }}
class InviteTest{ #[Test] public function theNameShowsAsText(): void { Invite::send('bo@example.com', 'Bo <b>Admin</b>');
// `<` and `>` are written as `<` and `>`, so the name is not a tag. Test::assertSame(Test::sentMail()[0]->html(), '<p>Hello Bo <b>Admin</b></p>');
// The plain-text body is made from the HTML. It has the name as it was given. Test::assertSame(Test::sentMail()[0]->text(), "Hello Bo <b>Admin</b>\n"); }} InviteTest ✓ theNameShowsAsText
0 failed, 1 passed, 0 skipped, 0 flakyA short notice has no html
<?nvs// Shows how to test that a short notice is sent as plain text, with no HTML body.// proof: test
use Core\Mail;use Core\Test;
class Login{ // Tells the user about a sign-in from a new device. The mail has only a plain-text body. public static function newDevice(string $email, string $device): void { Mail::send('site', [$email], 'New sign-in', 'Someone signed in from ' . $device . '.'); }}
class LoginTest{ #[Test] public function theNoticeIsPlainText(): void { Login::newDevice('ada@example.com', 'a phone');
// The body was a `string`, so `html()` returns `null`. Test::assertNull(Test::sentMail()[0]->html()); Test::assertSame(Test::sentMail()[0]->text(), 'Someone signed in from a phone.'); }} LoginTest ✓ theNoticeIsPlainText
0 failed, 1 passed, 0 skipped, 0 flakyBoth bodies have the same link
<?nvs// Shows how to test that a request sends a mail with the same link in the HTML body and the text body.// proof: test
use Core\Html;use Core\Html\Markup;use Core\Http\Method;use Core\Str;use Core\Test;
class InvoiceTest{ #[Test] public function bothBodiesHaveTheLink(): void { Test::request(Method::Post, '/invoices/77/send');
Test::assertCount(Test::sentMail(), 1); string $link = 'https://example.com/invoices/77.pdf';
// `html()` returns a `?string`. `?? ''` gives an empty string when it is `null`. string $html = Test::sentMail()[0]->html() ?? ''; Test::assertTrue(Str::contains($html, '<a href="' . $link . '">')); Test::assertTrue(Str::contains(Test::sentMail()[0]->text(), $link)); }}
// `Test::request` sends a request to this program. The code below, outside the class, runs for it.// It sends the invoice link in an HTML body. The plain-text body is made from the HTML.if (Core\Env::context() == Core\Env::Context::Request) { string $link = 'https://example.com/invoices/77.pdf'; Markup $body = ('<p><a href="' as Markup) + Html::escape($link) + ('">Download your invoice</a></p>' as Markup); Core\Mail::send('site', ['billing@example.com'], 'Invoice 77', $body); Core\Response::setStatus(202);} InvoiceTest ✓ bothBodiesHaveTheLink
0 failed, 1 passed, 0 skipped, 0 flaky