50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Core\Providers;
|
|
|
|
use Illuminate\Mail\Events\MessageSending;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Modules\Core\Store\Events\StoreDetailsUpdated;
|
|
use Modules\Core\Store\Listeners\FlushStoreDetailsCache;
|
|
use Modules\Core\Store\Services\StoreDetailsService;
|
|
use Symfony\Component\Mime\Address;
|
|
|
|
class StoreServiceProvider extends ServiceProvider
|
|
{
|
|
public function boot(): void
|
|
{
|
|
Event::listen(StoreDetailsUpdated::class, FlushStoreDetailsCache::class);
|
|
Event::listen(MessageSending::class, [$this, 'applyMailFromName']);
|
|
}
|
|
|
|
/**
|
|
* Renames the From header's display name to the store's own Store
|
|
* Details setting, when filled in — leaves the address itself alone
|
|
* (a deliverability/domain-authentication concern, not something a
|
|
* merchant should freely edit via a text field) and leaves an
|
|
* EXPLICIT sender alone too: anything whose From address isn't
|
|
* config('mail.from.address') deliberately chose a different sender
|
|
* (e.g. a mailable/notification that calls ->from() itself), which
|
|
* this provider has no business second-guessing.
|
|
* StoreDetailsService::current() is forever-cached (see its own
|
|
* docblock), so this costs nothing extra per send.
|
|
*/
|
|
public function applyMailFromName(MessageSending $event): void
|
|
{
|
|
$name = app(StoreDetailsService::class)->current()->mail_from_name;
|
|
|
|
if (blank($name)) {
|
|
return;
|
|
}
|
|
|
|
$from = $event->message->getFrom()[0] ?? null;
|
|
|
|
if ($from === null || $from->getAddress() !== config('mail.from.address')) {
|
|
return;
|
|
}
|
|
|
|
$event->message->from(new Address($from->getAddress(), $name));
|
|
}
|
|
}
|