CluPilotCloud/app/Mail/Concerns/SendsFromMailbox.php

38 lines
1.2 KiB
PHP

<?php
namespace App\Mail\Concerns;
use App\Services\Mail\MailboxResolver;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Envelope;
/**
* From and Reply-To, taken from the mailbox behind a purpose.
*
* Reply-To is the whole reason sending alone is enough: a customer answering a
* support reply lands in the support mailbox, read in an ordinary mail client,
* so no IMAP is needed to close the loop.
*
* Except on a no-reply mailbox. A "no-reply" address you can reply to is a lie
* in the sender, and the one place the promise is made is the address itself.
*/
trait SendsFromMailbox
{
protected function mailboxEnvelope(string $purpose, string $subject): Envelope
{
$box = app(MailboxResolver::class)->for($purpose);
if ($box === null) {
// No mailbox configured yet — fall back to the framework default so
// an installation mid-setup still sends rather than crashing.
return new Envelope(subject: $subject);
}
return new Envelope(
from: new Address($box->address, $box->display_name ?: null),
replyTo: $box->no_reply ? [] : [new Address($box->address, $box->display_name ?: null)],
subject: $subject,
);
}
}