Core\Mail\Message::inReplyTo
$message->inReplyTo(): ?stringReturns the message id in the In-Reply-To header of a received message, without its angle brackets.
A mail program writes this header when somebody answers a message. Its value is the message id of the answered message. If your program kept the id of each mail it sent, inReplyTo() tells you which one the reply is about. A new message has no In-Reply-To header, so the result is null. When the header has several ids, the result is the first one.
The result 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 in this header. So the id does not prove who sent the message, or that it answers your mail.
references() returns the ids of the whole conversation, and answers() combines both headers.
The examples below show how to find the sent mail of a reply, how to tell a reply from a new message, and a header with two ids.
Return value
Returns ?string. The message id, or null when the message has no In-Reply-To.
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
Find the order of a reply
<?nvs// Shows how to find which sent mail a customer answered.
use Core\Mail\Message;
// The shop kept the message id of each order mail it sent, with the order number.array<int> $sent = ['order-41@example.com' => 41, 'order-42@example.com' => 42];
Message $reply = Message::parse( "From: Ada <ada@example.org>\n" . "Subject: Re: Your order\n" . "In-Reply-To: <order-42@example.com>\n" . "\n" . "Can I change the address?\n",);
// `inReplyTo()` returns the id as a tainted string (text from outside your program).// Using a tainted string as an array key is allowed. This prints `order 42`.echo 'order ', $sent[$reply->inReplyTo() ?? ''] ?? 0, "\n";order 42A new message is not a reply
<?nvs// Shows how to tell a reply from a new message.
use Core\Mail\Message;
array<Message> $inbox = [ Message::parse("Subject: Re: Ticket 7\nIn-Reply-To: <ticket-7@example.com>\n\nIt works now."), Message::parse("Subject: My printer is broken\n\nPlease help."),];
// A new message has no `In-Reply-To` header, so `inReplyTo()` returns `null`.foreach ($inbox as Message $message) { if ($message->inReplyTo() == null) { echo $message->subject(), ": a new ticket\n"; } else { echo $message->subject(), ': a reply to ', $message->inReplyTo() ?? '', "\n"; }}Re: Ticket 7: a reply to ticket-7@example.comMy printer is broken: a new ticketOnly the first id is returned
<?nvs// Shows what `inReplyTo()` returns when the header has more than one message id.
use Core\Mail\Message;
// Some mail programs write two ids in `In-Reply-To`.Message $reply = Message::parse( "Subject: Re: Your order\n" . "In-Reply-To: <order-42@example.com> <welcome-1@example.com>\n" . "\n" . "Thanks!\n",);
// `inReplyTo()` returns the first id. This prints `order-42@example.com`.echo $reply->inReplyTo() ?? 'no id', "\n";
// `header()` returns the whole line, with both ids and their angle brackets.echo $reply->header('In-Reply-To')[0], "\n";order-42@example.com<order-42@example.com> <welcome-1@example.com>