Files
ladill-events/app/Services/Events/EventMailer.php
T
isaacclad 3d5ffa593d
Deploy Ladill Events / deploy (push) Successful in 48s
feat(messaging): suite-channel email/SMS and entitlement sync
Prefer platform suite messaging (channel=suite) for attendee email, fall back
to Bird integrations; sync suite plan entitlements on subscribe/renew.
2026-07-16 11:24:21 +00:00

114 lines
3.1 KiB
PHP

<?php
namespace App\Services\Events;
use App\Services\Billing\PlatformEmailClient;
use App\Services\Messaging\CustomerEmailClient;
use App\Services\Messaging\MessagingCredentialsService;
/**
* Attendee/speaker email: prefer platform suite messaging (zero-config mailbox),
* fall back to customer Bird credentials when suite is unavailable.
*/
class EventMailer
{
private ?string $lastError = null;
public function __construct(
private readonly MessagingCredentialsService $credentials,
private readonly CustomerEmailClient $customerEmail,
private readonly PlatformEmailClient $platformEmail,
) {}
public function lastError(): ?string
{
return $this->lastError;
}
public function isConfiguredForOwner(string $ownerPublicId): bool
{
if ($this->platformEmail->isConfigured()) {
return true;
}
return $this->credentials->forOwner($ownerPublicId)->hasValidBird();
}
/** @deprecated Use isConfiguredForOwner() */
public function isConfigured(): bool
{
return $this->platformEmail->isConfigured();
}
public function send(
string $ownerPublicId,
string $to,
string $subject,
string $html,
?string $text = null,
?string $replyToEmail = null,
?string $replyToName = null,
): bool {
$this->lastError = null;
if ($this->platformEmail->isConfigured()) {
$sent = $this->platformEmail->send($ownerPublicId, $to, $subject, $html, $text);
if ($sent) {
return true;
}
// Suite unavailable (mailbox, allowance) — try Bird power-user path next.
$suiteError = $this->platformEmail->lastError();
if ($this->tryCustomerBird($ownerPublicId, $to, $subject, $html, $text)) {
return true;
}
$this->lastError = $suiteError ?: $this->lastError;
return false;
}
return $this->tryCustomerBird($ownerPublicId, $to, $subject, $html, $text);
}
private function tryCustomerBird(
string $ownerPublicId,
string $to,
string $subject,
string $html,
?string $text,
): bool {
$credential = $this->credentials->forOwner($ownerPublicId);
if (! $credential->hasValidBird()) {
$this->lastError ??= 'Connect a Ladill mailbox in Account → Messaging, or connect Ladill Bird in Integrations.';
return false;
}
$apiKey = $credential->birdApiKey();
if (! $apiKey) {
$this->lastError = 'Bird credentials could not be decrypted. Reconnect Bird in Integrations.';
return false;
}
$sent = $this->customerEmail->send(
$apiKey,
(string) $credential->bird_from_email,
$credential->bird_from_name,
$to,
$subject,
$html,
$text,
);
if (! $sent) {
$this->lastError = $this->customerEmail->lastError() ?: 'The email could not be sent.';
return false;
}
return true;
}
}