Skip to content

Core\Mail\Message::kind

$message->kind(): Core\Mail\Message::Kind

Returns what a received message is: a bounce, a delay, a complaint, an auto-reply, a reply or another message.

A delivery report is a message that a mail server sends about a mail that you sent. It says whether each recipient got the mail. A bounce is a delivery report that says a mail could not be delivered. A complaint is a report from a mail provider that a reader marked your mail as spam.

The result is a Core\Mail\Message::Kind case. The message is checked when it is parsed, and the first rule that matches gives the kind. A delivery report with a failed recipient is a Bounce. A report with only late recipients is a Delay. A spam report is a Complaint. A message with an Auto-Submitted header, or a header that an out-of-office program writes, is an AutoReply. A message with In-Reply-To is a Reply. Everything else is Other.

Good to know: only the standard report formats count. A message with the subject "Undelivered" that is not a delivery report is Other. The kind never depends on the subject or the sender. So a stranger cannot make a normal message look like a bounce with its words.

Return value

Returns Core\Mail\Message::Kind. A Core\Mail\Message::Kind case.

Changelog

VersionChange
0.0.6Core\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

What each message is

<?nvs
// Shows the kind of four messages that arrive at a shop's address.
use Core\Mail\Message;
class Label
{
public static function of(Message::Kind $kind): string
{
return match ($kind) {
Message::Kind::Bounce => 'bounce',
Message::Kind::Delay => 'delay',
Message::Kind::Complaint => 'complaint',
Message::Kind::AutoReply => 'auto-reply',
Message::Kind::Reply => 'reply',
Message::Kind::Other => 'other',
};
}
}
array<string> $raw = [
// This message is a delivery report. The address ada@example.org does not exist.
"From: mailer-daemon@mx.example.net\n"
. "Subject: Undelivered Mail\n"
. "Content-Type: multipart/report; report-type=delivery-status; boundary=\"b\"\n\n"
. "--b\nContent-Type: message/delivery-status\n\n"
. "Reporting-MTA: dns; mx.example.net\n\n"
. "Final-Recipient: rfc822; ada@example.org\nAction: failed\nStatus: 5.1.1\n"
. "--b--\n",
// This message is an out-of-office reply. `Auto-Submitted` says that a program wrote it.
"From: bo@example.org\nAuto-Submitted: auto-replied\nSubject: Away until Monday\n\nI am away.\n",
// A person answers the order confirmation.
"From: cy@example.org\nIn-Reply-To: <order-42@example.com>\nSubject: Re: Your order\n\nThank you!\n",
// This message is a new question.
"From: dee@example.org\nSubject: Do you ship to Norway?\n\nHello.\n",
];
foreach ($raw as string $text) {
Message $message = Message::parse($text);
echo Label::of($message->kind()), ': ', $message->subject(), "\n";
}
Output
bounce: Undelivered Mail
auto-reply: Away until Monday
reply: Re: Your order
other: Do you ship to Norway?

A delay is not a bounce

<?nvs
// Shows that a late delivery is a `Delay`, and a failed delivery is a `Bounce`.
use Core\Mail\Message;
class Report
{
// Builds a delivery report with one recipient and the given action and status.
public static function with(string $action, string $status): string
{
return "From: mailer-daemon@mx.example.net\n"
. "Subject: Delivery Status Notification\n"
. "Content-Type: multipart/report; report-type=delivery-status; boundary=\"b\"\n\n"
. "--b\nContent-Type: message/delivery-status\n\n"
. "Reporting-MTA: dns; mx.example.net\n\n"
. "Final-Recipient: rfc822; ada@example.org\n"
. 'Action: ' . $action . "\nStatus: " . $status . "\n"
. "--b--\n";
}
}
// The mail has not reached ada's mail server yet. The server tries again later.
Message $late = Message::parse(Report::with('delayed', '4.4.1'));
echo $late->kind() == Message::Kind::Delay ? 'delay: wait, the mail may still arrive' : 'not a delay', "\n";
// Two days later the server gives up. Now the report says `failed`.
Message $failed = Message::parse(Report::with('failed', '4.4.7'));
echo $failed->kind() == Message::Kind::Bounce ? 'bounce: the mail did not arrive' : 'not a bounce', "\n";
// A report where every recipient got the mail is neither. Its kind is `Other`.
Message $delivered = Message::parse(Report::with('delivered', '2.0.0'));
echo $delivered->kind() == Message::Kind::Other ? 'other: nothing to do' : 'something else', "\n";
Output
delay: wait, the mail may still arrive
bounce: the mail did not arrive
other: nothing to do

Sort the support inbox

<?nvs
// A support inbox puts each message in a folder by its kind, so the team reads only real questions.
use Core\Mail\Message;
class Folder
{
public static function for(Message $message): string
{
return match ($message->kind()) {
Message::Kind::Bounce, Message::Kind::Delay => 'Delivery reports',
Message::Kind::Complaint => 'Spam complaints',
Message::Kind::AutoReply => 'Ignored',
Message::Kind::Reply => 'Open tickets',
Message::Kind::Other => 'New tickets',
};
}
}
array<string> $inbox = [
// A mail provider reports that a customer marked a mail as spam.
"From: abuse@mail.example.net\nSubject: Complaint\n"
. "Content-Type: multipart/report; report-type=feedback-report; boundary=\"f\"\n\n"
. "--f\nContent-Type: message/feedback-report\n\nFeedback-Type: abuse\n--f--\n",
// An out-of-office program wrote this message. It has the header `X-Autoreply`.
"From: bo@example.org\nX-Autoreply: yes\nSubject: Re: Ticket 7\nIn-Reply-To: <ticket-7@example.com>\n\nI am away.\n",
// The customer answers the support team.
"From: cy@example.org\nSubject: Re: Ticket 7\nIn-Reply-To: <ticket-7@example.com>\n\nIt works now.\n",
// A new customer writes for the first time.
"From: dee@example.org\nSubject: The app does not start\n\nHello, I need help.\n",
];
foreach ($inbox as string $raw) {
Message $message = Message::parse($raw);
echo Folder::for($message), ': ', $message->subject(), "\n";
}
Output
Spam complaints: Complaint
Ignored: Re: Ticket 7
Open tickets: Re: Ticket 7
New tickets: The app does not start