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, ?string $fromName = null, ): bool { $this->lastError = null; $displayName = $this->resolveFromName($ownerPublicId, $fromName, $replyToName); if ($this->platformEmail->isConfigured()) { $sent = $this->platformEmail->send($ownerPublicId, $to, $subject, $html, $text, $displayName); 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, $displayName)) { return true; } $this->lastError = $suiteError ?: $this->lastError; return false; } return $this->tryCustomerBird($ownerPublicId, $to, $subject, $html, $text, $displayName); } private function resolveFromName(string $ownerPublicId, ?string $fromName, ?string $replyToName): ?string { foreach ([$fromName, $replyToName] as $candidate) { $name = trim((string) $candidate); if ($name !== '') { return $name; } } $settings = AccountBranding::forOwnerRef($ownerPublicId); $company = trim(AccountBranding::companyName($settings)); return $company !== '' && strcasecmp($company, 'Company') !== 0 ? $company : null; } private function tryCustomerBird( string $ownerPublicId, string $to, string $subject, string $html, ?string $text, ?string $fromName = null, ): 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 ?: $fromName, $to, $subject, $html, $text, ); if (! $sent) { $this->lastError = $this->customerEmail->lastError() ?: 'The email could not be sent.'; return false; } return true; } }