Files
ladill-events/app/Services/Billing/PlatformEmailClient.php
T
isaaccladandCursor 1db499ba2f
Deploy Ladill Events / deploy (push) Successful in 44s
Surface platform SMTP errors on speaker invitation failures.
Return the Bird API error instead of a generic wallet message so domain and balance issues are actionable.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-03 16:23:09 +00:00

77 lines
2.3 KiB
PHP

<?php
namespace App\Services\Billing;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class PlatformEmailClient
{
private ?string $lastError = null;
public function lastError(): ?string
{
return $this->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() !== '';
}
}