Files
ladill-events/app/Services/Billing/PlatformEmailClient.php
T
isaacclad a7231ab16f
Deploy Ladill Events / deploy (push) Successful in 45s
fix(email): use client/org name as From display, not Ladill product
Customer-facing outbound mail should appear from the business (organizer,
clinic, company, location), not the Ladill product brand.
2026-07-16 12:01:06 +00:00

120 lines
4.0 KiB
PHP

<?php
namespace App\Services\Billing;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
/**
* Platform suite email (zero-config): POST /api/smtp/messages/send with channel=suite.
* No Bird API key or smtp_service_id required when platform messaging is enabled.
*/
class PlatformEmailClient
{
private ?string $lastError = null;
private ?string $lastErrorCode = null;
public function lastError(): ?string
{
return $this->lastError;
}
public function lastErrorCode(): ?string
{
return $this->lastErrorCode;
}
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 isConfigured(): bool
{
return $this->token() !== '';
}
public function messagingSettingsUrl(): string
{
return (string) config(
'smtp.messaging_settings_url',
'https://account.ladill.com/account/settings/messaging?reason=messaging_mailbox_required&from=suite',
);
}
public function send(
string $ownerPublicId,
string $to,
string $subject,
string $html,
?string $text = null,
?string $fromName = null,
?string $idempotencyKey = null,
): bool {
$this->lastError = null;
$this->lastErrorCode = null;
if (! $this->isConfigured()) {
$this->lastError = 'Outbound email is not configured on this Events instance.';
return false;
}
$key = $idempotencyKey ?: ('events-email-'.Str::uuid());
try {
$res = Http::withToken($this->token())
->withHeaders(['Idempotency-Key' => $key])
->acceptJson()
->timeout(30)
// from_name: pass the organizer/company display name. Do not default
// to EVENTS_SMTP_FROM_NAME ("Ladill Events") — attendee mail is from the customer.
->post($this->base().'/messages/send', array_filter([
'user' => $ownerPublicId,
'channel' => 'suite',
'from_name' => $fromName,
'to' => $to,
'subject' => $subject,
'html' => $html,
'text' => $text,
'reference' => $key,
], static fn ($v) => $v !== null && $v !== ''));
if ($res->status() === 402) {
$this->lastErrorCode = (string) ($res->json('error') ?: 'allowance_exceeded');
$this->lastError = (string) ($res->json('error') ?: 'Email allowance exceeded. Upgrade your plan or top up your wallet.');
Log::warning('Events suite email: allowance/balance', ['user' => $ownerPublicId, 'body' => $res->body()]);
return false;
}
if ($res->failed()) {
$code = (string) ($res->json('error') ?: 'send_failed');
$this->lastErrorCode = $code;
if ($code === 'messaging_mailbox_required') {
$this->lastError = 'Set a Ladill mailbox for suite email in Account → Messaging settings: '.$this->messagingSettingsUrl();
} else {
$this->lastError = (string) ($res->json('error') ?: 'The email could not be sent. Please try again.');
}
Log::warning('Events suite email 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('Events suite email error', ['error' => $e->getMessage()]);
return false;
}
}
}