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)); if (function_exists('curl_init')) { $curl = curl_init($url); curl_setopt_array($curl, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $body, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $timeout, ]); $response = curl_exec($curl); $status = (int) curl_getinfo($curl, CURLINFO_RESPONSE_CODE); $error = curl_error($curl); curl_close($curl); if ($response === false) { throw new ProtonException('Почтовый сервис недоступен: ' . $error); } return ['status' => $status, 'body' => (string) $response]; } $context = stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => implode("\r\n", $headers), 'content' => $body, 'timeout' => $timeout, // Нужен ответ, а не исключение: код разбираем сами 'ignore_errors' => true, ], ]); $response = @file_get_contents($url, false, $context); if ($response === false) { throw new ProtonException('Почтовый сервис недоступен: ' . $url); } $status = 0; foreach ($http_response_header ?? [] as $header) { if (preg_match('#^HTTP/\S+\s+(\d{3})#', $header, $matches) === 1) { $status = (int) $matches[1]; } } return ['status' => $status, 'body' => $response]; } }