toArray(); // Сервис ждёт получателей строкой или списком — отдаём список, // а пустые поля не шлём вовсе, чтобы не спорить с его валидацией $body = array_filter([ 'to' => $payload['to'], 'cc' => $payload['cc'], 'bcc' => $payload['bcc'], 'from' => $payload['from'], 'from_name' => $payload['from_name'], 'reply_to' => $payload['reply_to'], 'subject' => $payload['subject'], 'text' => $payload['text'], 'html' => $payload['html'], 'headers' => $payload['headers'], 'attachments' => $payload['attachments'], ], static fn (mixed $value): bool => $value !== '' && $value !== []); $response = $this->post( $url . '/api/v1/messages', (string) json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), [ 'Content-Type: application/json', 'Authorization: Bearer ' . $key, // Своя цепочка запросов доезжает до сервиса и находится в его логах RequestId::HEADER . ': ' . RequestId::current(), ] ); if ($response['status'] < 200 || $response['status'] >= 300) { $decoded = json_decode($response['body'], true); $detail = is_array($decoded) ? (string) ($decoded['error']['message'] ?? '') : ''; throw new ProtonException( 'Почтовый сервис ответил ' . $response['status'] . ($detail !== '' ? ': ' . $detail : '') ); } } public function name(): string { return 'mailer'; } /** * @param array $headers * * @return array{status: int, body: string} */ private function post(string $url, string $body, array $headers): array { $timeout = max(2, (int) Config::get('mail.service.timeout', 10)); try { return (new HttpClient($timeout))->post($url, $body, $headers); } catch (ProtonException $e) { throw new ProtonException('Почтовый сервис недоступен: ' . $e->getMessage()); } } }