Core\Mail\Message::references
Returns the message ids in the References header of a received message, without their angle brackets.
A mail program writes this header when somebody answers a message. It lists the message ids of the conversation, the oldest first. The first id is the message that started the conversation. The last id is usually the message that was answered. A message with no References header gives an empty array.
A long References header can continue on the next line, which starts with a space. references() reads all of these lines.
The sender wrote this header, so each id is a tainted string. Tainted means that the value comes from outside your program, so you must check or escape it before you use it. Using it as an array key is allowed. Anybody can write any id, so an id does not prove who sent the message.
inReplyTo() returns only the id of the message that was answered.
The examples below show the ids of a conversation, replies grouped by conversation, and a header that continues over several lines.
Return value
Returns array<string>. The ids, oldest first. The array is empty when the message has none.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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
The ids of a conversation
<?nvs// Shows how to read the message ids of the conversation that a reply belongs to.
use Core\Mail\Message;
Message $reply = Message::parse( "From: Ada <ada@example.org>\n" . "Subject: Re: Re: Your order\n" . "In-Reply-To: <answer-2@example.com>\n" . "References: <order-42@example.com> <question-1@example.org> <answer-2@example.com>\n" . "\n" . "Thank you, that helps.\n",);
// `references()` returns the ids without their angle brackets, the oldest first.foreach ($reply->references() as var $id) { echo $id, "\n";}order-42@example.comquestion-1@example.organswer-2@example.comGroup replies by conversation
<?nvs// Shows how to group received messages by the first message of their conversation.
use Core\Mail\Message;
array<Message> $inbox = [ Message::parse("Subject: Re: Order 42\nReferences: <order-42@example.com>\n\nWhen does it arrive?"), Message::parse("Subject: Re: Order 43\nReferences: <order-43@example.com>\n\nThanks."), Message::parse("Subject: Re: Re: Order 42\nReferences: <order-42@example.com> <a-7@example.com>\n\nIt arrived."), Message::parse("Subject: A question\n\nAre you open on Sunday?"),];
// The first id of `references()` is the message that started the conversation.// A new message has no `References` header, so its array is empty.foreach ($inbox as Message $message) { array<tainted string> $thread = $message->references(); echo Core\Arr::first($thread) ?? 'new', ': ', $message->subject(), "\n";}order-42@example.com: Re: Order 42order-43@example.com: Re: Order 43order-42@example.com: Re: Re: Order 42new: A questionThe length of a conversation
<?nvs// Shows how to count the messages that came before a reply in its conversation.
use Core\Mail\Message;
Message $reply = Message::parse( "Subject: Re: Ticket 7\n" . "References: <ticket-7@example.com>\n" . " <answer-1@example.com>\n" . " <question-2@example.org>\n" . "\n" . "It works now.\n",);
// A long `References` header continues on the next lines. Each of these lines starts with a space.// `references()` reads all of them. This prints `3 messages came before this reply`.echo Core\Arr::count($reply->references()), " messages came before this reply\n";3 messages came before this reply