Skip to content

Core\Mail\Message::replyTo

$message->replyTo(): array<Core\Mail\Address>

Returns the addresses in the Reply-To header of a received message, as an array of Core\Mail\Address.

A sender writes Reply-To when an answer should go to another address than the From address. A contact form on a website is a common case: the mail comes from the website, and the reply goes to the visitor. A message without a Reply-To header returns an empty array, and the reply then goes to the From address.

Good to know: the sender writes this header, so each address and name is tainted. Tainted means that the value comes from outside your program, so you must check or escape it before you use it. The sender can write any address in Reply-To. Your reply goes to that address.

The examples below read where a reply goes, use the sender when there is no Reply-To, and answer the visitor of a contact form.

Return value

Returns array<Core\Mail\Address>. The addresses, in order. The array is empty when the message has no Reply-To.

Changelog

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

Where a reply goes

<?nvs
// Shows how to read the address that a reply to a message should go to.
use Core\Mail\Message;
// Ada writes from her work address, and she wants the answer at home.
Message $mail = Message::parse(
"From: Ada <ada@work.example.org>\r\n"
. "Reply-To: Ada at home <ada@home.example.org>\r\n"
. "Subject: My order\r\n"
. "\r\n"
. 'Please answer me at home.',
);
// `from` returns the address of the `From` header.
// This prints `ada@work.example.org`.
echo 'From: ', $mail->from()?->address() ?? '', "\n";
// `replyTo` returns the addresses of the `Reply-To` header.
// This prints `ada@home.example.org (Ada at home)`.
echo 'Reply to: ', $mail->replyTo()[0]->address(), ' (', $mail->replyTo()[0]->name(), ")\n";
Output
From: ada@work.example.org
Reply to: ada@home.example.org (Ada at home)

Use the sender without reply to

<?nvs
// Shows how to choose the address for a reply when a message may have no `Reply-To` header.
use Core\Mail\Address;
use Core\Mail\Message;
class Reply
{
// This function returns the `Reply-To` addresses. When there are none, it returns the sender.
public static function addresses(Message $mail): array<Address>
{
array<Address> $addresses = $mail->replyTo();
?Address $sender = $mail->from();
if (Core\Arr::isEmpty($addresses) && $sender != null) {
return [$sender];
}
return $addresses;
}
}
Message $withHeader = Message::parse("From: news@example.org\r\nReply-To: team@example.org\r\n\r\nNews");
Message $withoutHeader = Message::parse("From: bo@example.org\r\n\r\nHello");
// This prints `team@example.org` and then `bo@example.org`.
echo Reply::addresses($withHeader)[0]->address(), "\n";
echo Reply::addresses($withoutHeader)[0]->address(), "\n";
Output
team@example.org
bo@example.org

Answer a contact form

<?nvs
// Shows how a shop answers a message from its contact form, which comes from the website itself.
use Core\Mail\Address;
use Core\Mail\Message;
// The website sends each form as a mail from `form@example.com`, with the visitor in `Reply-To`.
Message $form = Message::parse(
"From: Contact form <form@example.com>\r\n"
. "Reply-To: Cleo Martin <cleo@example.org>\r\n"
. "To: support@example.com\r\n"
. "Subject: Question about delivery\r\n"
. "\r\n"
. 'Do you deliver to Austria?',
);
// The answer goes to the visitor. The name is tainted, because the visitor typed it into the form.
// Tainted means that the value comes from outside your program, so you must check or escape it.
foreach ($form->replyTo() as Address $visitor) {
echo 'To: ', $visitor->address(), "\n";
echo 'Hello ', $visitor->name(), ", yes, we deliver to Austria.\n";
}
Output
To: cleo@example.org
Hello Cleo Martin, yes, we deliver to Austria.