Skip to content

Core\Mail\Message::cc

$message->cc(): array<Core\Mail\Address>

Returns the recipients in the Cc header of a received message, as an array of Core\Mail\Address.

Cc means "carbon copy". These are the people who got a copy of the message. The addresses are in the order of the header. A message without a Cc header returns an empty array.

Good to know: the sender writes the Cc header, so each address and name is tainted. Tainted means that the value comes from outside your program, so you must check or escape it before you use it. Escape it with Core\Html::escape before you put it on a page.

Bcc is a hidden copy. The people in Bcc do not appear in any header that the recipients see.

The examples below list the copied recipients, show a message without Cc, and collect the addresses for a reply to everyone.

Return value

Returns array<Core\Mail\Address>. One address for each recipient, in order. The array is empty when there is none.

Changelog

VersionChange
0.0.6Core\Mail\Message::parse reads a raw email, such as an .eml file or the body of a webhook. It returns the headers, the text and HTML bodies and the attachments, and a broken message does not throw an error. Every value is tainted: it comes from outside your program, so you must check or escape it.

Examples

Who got a copy

<?nvs
// Shows how to list the people who got a copy of a received message.
use Core\Mail\Address;
use Core\Mail\Message;
Message $mail = Message::parse(
"From: ada@example.org\r\n"
. "To: support@example.com\r\n"
. "Cc: Bo Chen <bo@example.org>, accounts@example.org\r\n"
. "Subject: Invoice 1042\r\n"
. "\r\n"
. 'The invoice has the wrong address.',
);
// `cc` returns one address for each person in the `Cc` header, in order.
// This prints `bo@example.org` and `accounts@example.org`.
foreach ($mail->cc() as Address $copy) {
echo $copy->address(), "\n";
}
Output
bo@example.org
accounts@example.org

A message without cc

<?nvs
// Shows what `cc` returns for a message that has no `Cc` header.
use Core\Mail\Message;
Message $mail = Message::parse("From: ada@example.org\r\nTo: support@example.com\r\nSubject: Hi\r\n\r\nHello");
// The header is missing, so the result is an empty array. This prints `0`.
echo Core\Arr::count($mail->cc()), "\n";
// `Core\Arr::isEmpty` checks whether the array is empty. This prints `no copies`.
echo Core\Arr::isEmpty($mail->cc()) ? 'no copies' : 'copies', "\n";
Output
0
no copies

Reply to everyone

<?nvs
// Shows how a help desk collects the addresses for a reply to everyone on a message.
use Core\Mail\Address;
use Core\Mail\Message;
Message $mail = Message::parse(
"From: Ada <ada@example.org>\r\n"
. "To: support@example.com, bo@example.org\r\n"
. "Cc: Cleo <cleo@example.org>, support@example.com\r\n"
. "Subject: Delivery date\r\n"
. "\r\n"
. 'When does the order arrive?',
);
// The reply goes to the sender, every `To` recipient and every `Cc` recipient.
array<Address> $everyone = Core\Arr::appendAll($mail->to(), $mail->cc());
?Address $sender = $mail->from();
if ($sender != null) {
$everyone = Core\Arr::prepend($everyone, $sender);
}
// This loop skips the help desk's own address. It also skips each address that is already in the list.
array<tainted string> $reply = [];
foreach ($everyone as Address $person) {
tainted string $address = Core\Str::lower($person->address());
if ($address != 'support@example.com' && !Core\Arr::contains($reply, $address)) {
$reply[] = $address;
}
}
// This prints `ada@example.org, bo@example.org, cleo@example.org`.
echo Core\Str::join($reply, ', '), "\n";
Output
ada@example.org, bo@example.org, cleo@example.org