lastError; } private function base(): string { return rtrim((string) config('smtp.platform_api_url', 'https://ladill.com/api/smtp'), '/'); } private function token(): string { return (string) config('smtp.platform_api_key', ''); } public function send(string $ownerPublicId, string $to, string $subject, string $html, ?string $text = null): bool { $this->lastError = null; if (! $this->isConfigured()) { $this->lastError = 'Outbound email is not configured on this Events instance.'; return false; } try { $res = Http::withToken($this->token())->acceptJson()->timeout(30) ->post($this->base().'/messages/send', array_filter([ 'user' => $ownerPublicId, 'from' => config('smtp.from'), 'from_name' => config('smtp.from_name'), 'to' => $to, 'subject' => $subject, 'html' => $html, 'text' => $text, ])); if ($res->status() === 402) { $this->lastError = (string) ($res->json('error') ?: 'Insufficient Bird email credits. Add funds in Billing and try again.'); Log::warning('Platform email send: insufficient balance', ['user' => $ownerPublicId]); return false; } if ($res->failed()) { $this->lastError = (string) ($res->json('error') ?: 'The email could not be sent. Please try again.'); Log::warning('Platform email send failed', ['status' => $res->status(), 'body' => $res->body()]); return false; } return (bool) ($res->json('success') ?? true); } catch (\Throwable $e) { $this->lastError = 'The email could not be sent. Please try again.'; Log::warning('Platform email send error', ['error' => $e->getMessage()]); return false; } } public function isConfigured(): bool { return $this->token() !== ''; } }