Skip to content

Core\Test\SentMail::subject

$sentMail->subject(): string

subject() returns the subject line of a message. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and subject() is one of its methods.

The subject is the $subject string that your program gave to Core\Mail::send. It is exactly that string. No text is added to it, and no text is removed from it.

Use subject() to check the line a person sees first in their inbox. A good subject has the information that the person looks for, such as an order number or the name of a ticket.

The examples below show a subject with an order number in it, a long product name that is shortened to fit the subject, and a support ticket form whose title goes into the subject.

Return value

Returns string. The $subject given to Core\Mail::send.

Changelog

VersionChange
0.0.5Core\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.

Examples

The examples below run with this configuration file.

nvs.toml
# 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 = 1
from = "hello@example.com"
timeout = "1s"
# The program may send mail through the `site` block.
[capabilities.mail]
send = ["site"]

The subject has the order number

<?nvs
// Shows how to test that the subject of a shipping mail has the order number in it.
// proof: test
use Core\Mail;
use Core\Test;
class Shipping
{
// Tells the customer that their order has left the warehouse.
public static function shipped(string $customer, int $orderNumber): void
{
Mail::send('site', [$customer], 'Your order #' . $orderNumber . ' is on its way', 'It arrives in 3 days.');
}
}
class ShippingTest
{
#[Test]
public function theSubjectNamesTheOrder(): void
{
Shipping::shipped('ada@example.com', 1042);
// `assertSame` checks the whole subject, character by character.
Test::assertSame(Test::sentMail()[0]->subject(), 'Your order #1042 is on its way');
}
}
Output
ShippingTest
✓ theSubjectNamesTheOrder
0 failed, 1 passed, 0 skipped, 0 flaky

A long product name is shortened

<?nvs
// Shows how to test that a long product name is shortened so that the subject stays short.
// proof: test
use Core\Mail;
use Core\Str;
use Core\Test;
class BackInStock
{
// Uses the first 30 characters of the name, and adds `...` when the name is longer.
public static function notify(string $customer, string $product): void
{
string $name = Str::length($product) > 30 ? Str::slice($product, 0, 30) . '...' : $product;
Mail::send('site', [$customer], 'Back in stock: ' . $name, $product . ' is back in stock.');
}
}
class BackInStockTest
{
#[Test]
public function aLongNameIsShortened(): void
{
BackInStock::notify('ada@example.com', 'Wireless noise-cancelling headphones with case');
// The subject is 15 + 30 + 3 = 48 characters long, and it ends with `...`.
string $subject = Test::sentMail()[0]->subject();
Test::assertSame(Str::length($subject), 48);
Test::assertTrue(Str::endsWith($subject, '...'));
}
// A short name is used as it is.
#[Test]
public function aShortNameStaysTheSame(): void
{
BackInStock::notify('ada@example.com', 'Blue mug');
Test::assertSame(Test::sentMail()[0]->subject(), 'Back in stock: Blue mug');
}
}
Output
BackInStockTest
✓ aLongNameIsShortened
✓ aShortNameStaysTheSame
0 failed, 2 passed, 0 skipped, 0 flaky

The ticket title goes into the subject

<?nvs
// Shows how to test that a support form puts the title of the ticket into the subject.
// proof: test
use Core\Http\Method;
use Core\Test;
class TicketTest
{
// The title in the form has spaces around it. The subject has the title without them.
#[Test]
public function theSubjectHasTheTitle(): void
{
Test::request(Method::Post, '/tickets', {
headers: ['content-type' => 'application/x-www-form-urlencoded'],
body: 'title=++Login+does+not+work++&message=Help',
});
Test::assertCount(Test::sentMail(), 1);
Test::assertSame(Test::sentMail()[0]->subject(), 'New ticket: Login does not work');
}
}
// `Core\Test::request` runs this file again for the request. It sends the ticket to support.
// Form text is a `tainted string` (text from a visitor). `Core\Mail::send` accepts it.
if (Core\Env::context() == Core\Env::Context::Request) {
tainted string $title = Core\Str::trim(Core\Request::formField('title') ?? '');
tainted string $message = Core\Request::formField('message') ?? '';
Core\Mail::send('site', ['support@example.com'], 'New ticket: ' . $title, $message);
Core\Response::setStatus(201);
}
Output
TicketTest
✓ theSubjectHasTheTitle
0 failed, 1 passed, 0 skipped, 0 flaky