Core\Mail\Message::answers
$message->answers(): ?stringReturns the Message-ID of the mail that a received message is about, without the angle brackets.
A bounce is a message from a mail server that says your mail could not be delivered. A bounce, a delay and a spam complaint contain the headers of your mail, and the id comes from there. For any other message, the id is the In-Reply-To header. When there is none, it is the last id in References. Otherwise the result is null.
Core\Mail::send returns the Message-ID of each mail it sends. Save it with your record of the mail, so you can find that record when a bounce or an answer arrives.
The result is tainted. 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.
Good to know: some servers send a bounce without a copy of your mail. Then answers() is null. Pass the bounceTag option to send. Then bounceTag() returns that tag from the bounce.
The examples below show a script that reads a mailbox, a reply found by its headers, and a spam complaint that stops a newsletter.
Return value
Returns ?string. The id, or null when the message names none.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Mail\Message::kind returns what kind of message it is: a bounce, a delay, a complaint, an automatic reply, a reply or other. A bounce is a message from a mail server that says your mail could not be delivered. answers() returns the message id of the mail it is about, so you can find the mail you sent. |
Examples
The examples below run with this configuration file.
# The shop sends its mail from noreply@example.com. Bounces and answers arrive in# that mailbox. Here the mailbox is a folder on disk, with one `.eml` file per message.[imap.noreply]directory = "mailbox"
# The program may read the `noreply` mailbox, and the files of its folder.[capabilities.imap]connect = ["noreply"]
[capabilities.fs]read = ["mailbox"]A scheduled script reads the noreply mailbox
<?nvs// A scheduled script reads the noreply mailbox and marks each sent mail as bounced or answered.// A `[[schedule]]` entry in `nvs.toml` can run it every ten minutes.
use Core\Mail\Message;
class Sent{ // `tag` finds the tag of a received message in the shop's own list, and returns the shop's copy. // It returns `null` when the shop never sent a mail with this tag. public static function tag(array<string> $status, ?tainted string $written): ?string { foreach ($status as string $tag => string $state) { if ($tag == $written) { return $tag; } } return null; }}
// This array lists the mails the shop sent. The key is the `Message-ID` that `Core\Mail::send` returned.// The shop sent each mail with a `bounceTag`. The value is that tag.array<string> $tagOf = [ 'order-41@example.com' => 'order-41', 'order-42@example.com' => 'order-42', 'order-43@example.com' => 'order-43', 'order-44@example.com' => 'order-44',];array<string> $status = [ 'order-41' => 'sent', 'order-42' => 'sent', 'order-43' => 'sent', 'order-44' => 'sent',];
var $inbox = Core\Imap::open('noreply')->folder('INBOX');foreach ($inbox->search() as int $uid) { Message $message = $inbox->fetch($uid); Message::Kind $kind = $message->kind(); if ($kind != Message::Kind::Bounce && $kind != Message::Kind::Reply) { echo $uid, ': skipped, it is not a bounce or a reply', "\n"; continue; }
// `answers()` gives the `Message-ID` of the shop's mail. Some bounces have no copy of the mail. // For such a bounce, the program finds the mail by `bounceTag()`. ?tainted string $id = $message->answers(); ?string $tag = $id != null ? ($tagOf[$id] ?? null) : null; $tag = $tag ?? Sent::tag($status, $message->bounceTag()); if ($tag == null) { echo $uid, ': no mail of the shop matches', "\n"; continue; } $status[$tag] = $kind == Message::Kind::Bounce ? 'bounced' : 'answered'; echo $uid, ': ', $tag, ' is ', $status[$tag], "\n";}
foreach ($status as string $order => string $state) { echo $order, ' ', $state, "\n";}1: order-42 is bounced2: order-43 is answered3: skipped, it is not a bounce or a reply4: order-44 is bouncedorder-41 sentorder-42 bouncedorder-43 answeredorder-44 bouncedThe headers of a reply
<?nvs// Shows which header `answers()` reads for a reply.
use Core\Mail\Message;
// `In-Reply-To` names the mail that the person answers. This prints `ticket-7@example.com`.Message $reply = Message::parse( "From: cy@example.org\nSubject: Re: Ticket 7\n" . "In-Reply-To: <ticket-7@example.com>\n" . "References: <ticket-1@example.com> <ticket-7@example.com>\n\nThanks.\n",);echo $reply->answers() ?? 'none', "\n";
// Some programs write only `References`. `answers()` returns the last id in `References`. This prints `ticket-9@example.com`.Message $forwarded = Message::parse( "From: dee@example.org\nSubject: Fw: Ticket 9\n" . "References: <ticket-8@example.com> <ticket-9@example.com>\n\nSee below.\n",);echo $forwarded->answers() ?? 'none', "\n";
// A new message names no other mail, so the result is `null`.Message $new = Message::parse("From: eve@example.org\nSubject: Hello\n\nA question.\n");echo $new->answers() ?? 'none', "\n";ticket-7@example.comticket-9@example.comnoneA complaint stops the newsletter
<?nvs// A spam complaint names the newsletter it is about, and the shop stops that newsletter for the reader.
use Core\Mail\Message;
// This array lists the newsletters the shop sent. The key is the `Message-ID` that `Core\Mail::send` returned.array<string> $sentTo = [ 'news-0611-ada@example.com' => 'ada@example.org', 'news-0611-bo@example.com' => 'bo@example.org',];
// A mail provider sends this report when a reader marks the newsletter as spam.// The last part of the report is a copy of the newsletter.Message $complaint = Message::parse( "From: feedback@mail.example.net\nSubject: Complaint about message\n" . "Content-Type: multipart/report; report-type=feedback-report; boundary=\"f\"\n\n" . "--f\nContent-Type: text/plain\n\nA user marked this message as spam.\n" . "--f\nContent-Type: message/feedback-report\n\nFeedback-Type: abuse\nVersion: 1\n\n" . "--f\nContent-Type: message/rfc822\n\n" . "From: Shop <news@example.com>\nTo: bo@example.org\nSubject: Our June offers\n" . "Message-ID: <news-0611-bo@example.com>\n\nTen percent off.\n" . "--f--\n",);
if ($complaint->kind() == Message::Kind::Complaint) { ?tainted string $id = $complaint->answers(); ?string $reader = $id != null ? ($sentTo[$id] ?? null) : null; echo $reader != null ? 'No more newsletters for ' . $reader : 'The complaint is about no mail of the shop', "\n";}No more newsletters for bo@example.org