Skip to content

Core\Test\SentMail::cc

$sentMail->cc(): array<string>

cc() returns the list of addresses that get a copy of a message. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and cc() is one of its methods.

The list contains the cc addresses that your program gave to Core\Mail::send, in the same order. When the program gave no cc addresses, the list is empty. Every person who gets the message can see the cc addresses.

Use cc() to check that the right people get a copy, and that nobody else does.

The examples below show an invoice with a copy for the accounts team, a shipping notice with no copy, and a refund request that a request sends with a copy for a manager.

Return value

Returns array<string>. The cc addresses, in the order given. The list is empty when none were given.

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 accounts team gets a copy of each invoice

<?nvs
// Shows how to test that the accounts team gets a copy of each invoice.
// proof: test
use Core\Mail;
use Core\Test;
class Billing
{
// Sends the invoice to the customer, with a copy for the accounts team.
public static function sendInvoice(string $customer, int $number): void
{
Mail::send('site', [$customer], 'Invoice ' . $number, 'Your invoice is attached.', {
cc: ['accounts@example.com'],
});
}
}
class BillingTest
{
#[Test]
public function theAccountsTeamGetsACopy(): void
{
Billing::sendInvoice('ada@example.com', 2031);
// The customer is in `to()`, and the accounts team is in `cc()`.
var $mail = Test::sentMail()[0];
Test::assertEquals($mail->to(), ['ada@example.com']);
Test::assertEquals($mail->cc(), ['accounts@example.com']);
}
}
Output
BillingTest
✓ theAccountsTeamGetsACopy
0 failed, 1 passed, 0 skipped, 0 flaky

A shipping notice has no copy

<?nvs
// Shows how to test that a mail has no copy for anybody else.
// proof: test
use Core\Mail;
use Core\Test;
class Shipping
{
// Tells the customer that the parcel is on its way. Nobody else gets this mail.
public static function notify(string $customer, string $tracking): void
{
Mail::send('site', [$customer], 'Your parcel is on its way', 'Tracking number: ' . $tracking);
}
}
class ShippingTest
{
#[Test]
public function nobodyElseGetsACopy(): void
{
Shipping::notify('bo@example.com', 'TRK-7731');
// The program gave no `cc` addresses, so `cc()` returns an empty list.
Test::assertEquals(Test::sentMail()[0]->cc(), []);
Test::assertCount(Test::sentMail()[0]->cc(), 0);
}
}
Output
ShippingTest
✓ nobodyElseGetsACopy
0 failed, 1 passed, 0 skipped, 0 flaky

A manager gets a copy of a large refund

<?nvs
// Shows how to test that a manager gets a copy when a request asks for a large refund.
// proof: test
use Core\Http\Method;
use Core\Test;
class RefundTest
{
// A refund over 500 needs a manager, so the manager gets a copy.
#[Test]
public function aLargeRefundIsCopiedToTheManager(): void
{
Test::request(Method::Post, '/refunds', {
headers: ['content-type' => 'application/x-www-form-urlencoded'],
body: 'order=1042&amount=900',
});
Test::assertCount(Test::sentMail(), 1);
Test::assertEquals(Test::sentMail()[0]->cc(), ['manager@example.com']);
}
// A small refund goes to the refunds team alone. The list of copies is empty.
#[Test]
public function aSmallRefundHasNoCopy(): void
{
Test::request(Method::Post, '/refunds', {
headers: ['content-type' => 'application/x-www-form-urlencoded'],
body: 'order=1043&amount=20',
});
Test::assertCount(Test::sentMail(), 1);
Test::assertEquals(Test::sentMail()[0]->cc(), []);
}
}
// `Core\Test::request` runs this file again for the request. This part sends
// the refund request to the refunds team. A large amount also goes to the manager.
if (Core\Env::context() == Core\Env::Context::Request) {
int $order = (Core\Request::formField('order') ?? '0') as ?int ?? 0;
int $amount = (Core\Request::formField('amount') ?? '0') as ?int ?? 0;
array<string> $copies = [];
if ($amount > 500) {
$copies[] = 'manager@example.com';
}
Core\Mail::send('site', ['refunds@example.com'], 'Refund for order ' . $order, 'Amount: ' . $amount, {
cc: $copies,
});
Core\Response::setStatus(202);
}
Output
RefundTest
✓ aLargeRefundIsCopiedToTheManager
✓ aSmallRefundHasNoCopy
0 failed, 2 passed, 0 skipped, 0 flaky