Files
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

133 lines
3.9 KiB
PHP

<?php
namespace App\Services\Events;
use App\Services\Billing\PlatformEmailClient;
use App\Services\Messaging\CustomerEmailClient;
use App\Services\Messaging\MessagingCredentialsService;
use App\Support\AccountBranding;
/**
* 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,
?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;
}
}