Skip to content

Core\Test\SentMail::bcc

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

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

The list contains the bcc addresses that your program gave to Core\Mail::send, in the same order. When the program gave no bcc addresses, the list is empty. A person who gets the message cannot see the bcc addresses. The test can see all of them.

Use bcc() to check that addresses which must stay private are not in to() or cc().

The examples below show a newsletter whose readers are all in bcc, an order mail with a hidden copy for an archive, and invitations that a request sends.

Return value

Returns array<string>. The bcc 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"]

Newsletter readers do not see each other

<?nvs
// Shows how to test that the readers of a newsletter cannot see each other's addresses.
// proof: test
use Core\Mail;
use Core\Test;
class Newsletter
{
// Sends one message. `to` is the newsletter's own address, and every reader is in `bcc`.
public static function send(array<string> $readers, string $text): void
{
Mail::send('site', ['news@example.com'], 'Our news', $text, { bcc: $readers });
}
}
class NewsletterTest
{
#[Test]
public function theReadersAreHidden(): void
{
Newsletter::send(['ada@example.com', 'bo@example.com'], 'New products this month.');
// The readers are in `bcc()`. `to()` and `cc()` do not contain any reader.
var $mail = Test::sentMail()[0];
Test::assertEquals($mail->bcc(), ['ada@example.com', 'bo@example.com']);
Test::assertEquals($mail->to(), ['news@example.com']);
Test::assertEquals($mail->cc(), []);
}
}
Output
NewsletterTest
✓ theReadersAreHidden
0 failed, 1 passed, 0 skipped, 0 flaky

An archive keeps a hidden copy

<?nvs
// Shows how to test that an archive address gets a hidden copy of every order mail.
// proof: test
use Core\Mail;
use Core\Test;
class Orders
{
// Sends the order mail to the customer. The archive gets a copy the customer does not see.
public static function confirm(string $customer, int $number): void
{
Mail::send('site', [$customer], 'Order ' . $number, 'We received your order.', {
bcc: ['archive@example.com'],
});
}
}
class OrdersTest
{
#[Test]
public function everyOrderMailIsArchived(): void
{
Orders::confirm('ada@example.com', 1);
Orders::confirm('bo@example.com', 2);
// Each of the two messages has the archive in `bcc()`, and only the customer in `to()`.
Test::assertCount(Test::sentMail(), 2);
foreach (Test::sentMail() as var $mail) {
Test::assertEquals($mail->bcc(), ['archive@example.com']);
Test::assertCount($mail->to(), 1);
}
}
}
Output
OrdersTest
✓ everyOrderMailIsArchived
0 failed, 1 passed, 0 skipped, 0 flaky

A request sends party invitations

<?nvs
// Shows how to test the invitations that a request sends to a list of guests.
// proof: test
use Core\Http\Method;
use Core\Test;
class InvitationTest
{
// The host invites the guests of a saved list. Each guest is in `bcc`.
#[Test]
public function theGuestsAreInBcc(): void
{
Test::request(Method::Post, '/party/invite', {
headers: ['content-type' => 'application/x-www-form-urlencoded'],
body: 'list=friends',
});
Test::assertCount(Test::sentMail(), 1);
var $mail = Test::sentMail()[0];
Test::assertEquals($mail->bcc(), ['ada@example.com', 'bo@example.com', 'cy@example.com']);
Test::assertEquals($mail->to(), ['host@example.com']);
}
// A list that does not exist sends nothing.
#[Test]
public function anUnknownListSendsNothing(): void
{
Test::request(Method::Post, '/party/invite', {
headers: ['content-type' => 'application/x-www-form-urlencoded'],
body: 'list=strangers',
});
Test::assertCount(Test::sentMail(), 0);
}
}
// `Core\Test::request` runs this file again for the request. This part finds the
// saved list of guests and sends one message. The host is in `to`, and the guests are in `bcc`.
if (Core\Env::context() == Core\Env::Context::Request) {
array<string> $friends = ['ada@example.com', 'bo@example.com', 'cy@example.com'];
if (Core\Request::formField('list') == 'friends') {
Core\Mail::send('site', ['host@example.com'], 'You are invited', 'Party on Saturday at 8.', {
bcc: $friends,
});
}
Core\Response::setStatus(202);
}
Output
InvitationTest
✓ theGuestsAreInBcc
✓ anUnknownListSendsNothing
0 failed, 2 passed, 0 skipped, 0 flaky