Core\Mail\Message::messageId
$message->messageId(): ?stringReturns the Message-ID header of a received message, without its angle brackets.
The program that sent the message writes this id, such as <1f2e@example.com>. The result is 1f2e@example.com. A message without a Message-ID header returns null. When the header has more than one id, the result is the first one.
A reply names the id of the message it answers. So you can use the id to find the replies to a message, or to notice a message that you received twice.
Good to know: the id is tainted, because the sender wrote it. Tainted means that the value comes from outside your program, so you must check or escape it before you use it. It cannot be part of a file path or an SQL query without a check. Using it as an array key is allowed.
Two different senders can write the same id. Use it to find a message. Do not use it as proof of who sent the message.
Return value
Returns ?string. The message id, or null 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 id of a message
<?nvs// Shows how to read the id of a received message.
use Core\Mail\Message;
Message $mail = Message::parse( "From: ada@example.org\r\n" . "Subject: Opening hours\r\n" . "Message-ID: <5d41@mail.example.org>\r\n" . "\r\n" . 'Are you open on Sunday?',);
// `messageId` returns the id without the angle brackets.// This prints `5d41@mail.example.org`.echo $mail->messageId() ?? 'no id', "\n";5d41@mail.example.orgA message without an id
<?nvs// Shows what `messageId` returns for a message that has no `Message-ID` header.
use Core\Mail\Message;
Message $mail = Message::parse("From: printer@example.org\r\nSubject: Scan done\r\n\r\nYour scan is ready.");
// The header is missing, so the result is `null`. This prints `no id`.echo $mail->messageId() ?? 'no id', "\n";
// A message without an id can still get a key: the SHA-256 digest of its raw bytes.// The same bytes always give the same key. This prints the first 16 hex digits of the digest.tainted string $key = $mail->messageId() ?? Core\Encoding::toHex(Core\Hash::of($mail->raw(), Core\Hash::Digest::Sha256));echo Core\Str::slice($key, 0, 16), "\n";no ideb925fca21e17b33Skip a message received twice
<?nvs// Shows how a support inbox skips a message that a mail provider delivered twice.
use Core\Mail\Message;
// The provider sent the first message again, so its id appears two times.array<string> $deliveries = [ "Message-ID: <a1@example.org>\r\nSubject: Broken lamp\r\n\r\nThe lamp arrived broken.", "Message-ID: <b2@example.org>\r\nSubject: Invoice\r\n\r\nPlease send the invoice again.", "Message-ID: <a1@example.org>\r\nSubject: Broken lamp\r\n\r\nThe lamp arrived broken.",];
// `$seen` is the list of ids that were already handled. It is `array<tainted string>`.// The sender wrote the ids, so they are tainted (text from outside your program).array<tainted string> $seen = [];foreach ($deliveries as string $raw) { Message $mail = Message::parse($raw); tainted string $id = $mail->messageId() ?? ''; if (Core\Arr::contains($seen, $id)) { echo 'skipped ', $id, "\n"; continue; } $seen[] = $id; echo 'new ticket: ', $mail->subject(), "\n";}// This prints two new tickets and one skipped message.new ticket: Broken lampnew ticket: Invoiceskipped a1@example.org