Skip to content

Core\Test\SentMail::isAutoSubmitted

$sentMail->isAutoSubmitted(): bool

isAutoSubmitted() checks whether a message has the header Auto-Submitted: auto-generated. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and isAutoSubmitted() is one of its methods.

The header says that a program sent the message, not a person. A mail server does not send an out-of-office reply to such a message. So an automatic message does not start a loop of automatic replies.

The result is the autoSubmitted option that your program gave to Core\Mail::send. The default is true, because most mail that a web application sends is automatic: a receipt, a password reset or an alert. Give autoSubmitted: false for a message that a person wrote, such as a reply from a support agent.

Use isAutoSubmitted() to check that a message a person wrote is sent as a normal mail.

The examples below show an automatic order mail, a helpdesk reply that a person wrote, and a request that sends both kinds.

Return value

Returns bool. The autoSubmitted option. It is true when the option was not given.

Changelog

VersionChange
0.0.6Core\Test\SentMail reads every part of a sent message: its Message-ID, the sender's name, the attachments, the thread headers, the unsubscribe link and the bounce tag.

Examples

The examples below run with this configuration file.

nvs.toml
# The examples send mail through this block. `capture = true` keeps each message
# and sends nothing, so no mail leaves the computer.
[mail.shop]
capture = true
from = "shop@example.com"
from_name = "Example Shop"
# The program may send mail through the `shop` block.
[capabilities.mail]
send = ["shop"]

An order mail is automatic

<?nvs
// Shows how to test that an order confirmation has a header that says a program sent it.
// proof: test
use Core\Mail;
use Core\Test;
class Order
{
// Sends the confirmation with no `autoSubmitted` option.
public static function confirm(string $email): void
{
Mail::send('shop', [$email], 'Order 1042 is confirmed', 'Thank you for your order.');
}
}
class OrderTest
{
#[Test]
public function theConfirmationIsAutomatic(): void
{
Order::confirm('anna@example.com');
// The default is `true`. The message has the header `Auto-Submitted: auto-generated`,
// so an out-of-office reply is not sent back to the shop.
Test::assertTrue(Test::sentMail()[0]->isAutoSubmitted());
}
}
Output
OrderTest
✓ theConfirmationIsAutomatic
0 failed, 1 passed, 0 skipped, 0 flaky

A helpdesk answer a person wrote

<?nvs
// Shows how to test that a helpdesk reply that a person wrote has no `Auto-Submitted` header.
// proof: test
use Core\Mail;
use Core\Test;
class Helpdesk
{
// Sends the reply that a support agent typed. `autoSubmitted: false` leaves out
// the `Auto-Submitted` header, so the customer's mail program treats it as a normal mail.
public static function answer(string $email, string $text): void
{
Mail::send('shop', [$email], 'Re: My parcel is late', $text, {
fromName: 'Carla at Example Shop',
autoSubmitted: false,
});
}
}
class HelpdeskTest
{
#[Test]
public function theAnswerIsNotAutomatic(): void
{
Helpdesk::answer('anna@example.com', 'Your parcel arrives tomorrow.');
Test::assertTrue(!Test::sentMail()[0]->isAutoSubmitted());
}
}
Output
HelpdeskTest
✓ theAnswerIsNotAutomatic
0 failed, 1 passed, 0 skipped, 0 flaky

An answer and its copy

<?nvs
// Shows how to test a request that sends an agent's reply and an automatic copy to the team.
// proof: test
use Core\Http\Method;
use Core\Test;
class AnswerTest
{
#[Test]
public function onlyTheCopyIsAutomatic(): void
{
Test::request(Method::Post, '/tickets/381/answer');
// The first message is the reply that the agent wrote.
Test::assertTrue(!Test::sentMail()[0]->isAutoSubmitted());
// The second message is the copy that the program made.
Test::assertTrue(Test::sentMail()[1]->isAutoSubmitted());
}
}
// `Test::request` sends a request to this program. The code below, outside the class, runs for it.
// It sends the reply to the customer, and then a copy to the support team.
if (Core\Env::context() == Core\Env::Context::Request) {
string $answer = 'Your parcel arrives tomorrow.';
Core\Mail::send('shop', ['anna@example.com'], 'Re: Ticket 381', $answer, { autoSubmitted: false });
Core\Mail::send('shop', ['support@example.com'], 'Ticket 381 was answered', 'Carla wrote: ' . $answer);
Core\Response::setStatus(202);
}
Output
AnswerTest
✓ onlyTheCopyIsAutomatic
0 failed, 1 passed, 0 skipped, 0 flaky