getenv('MAILCORE_FROM') ?: 'Example Shop ', 'to' => getenv('MAILCORE_TO') ?: 'customer@recipient.example', 'subject' => 'Order #8421 completed', 'text' => 'Your order has been completed.', 'tags' => ['order_id' => '8421'], ]; if (str_contains($payload['from'], '.example') || str_ends_with($payload['to'], '.example')) { fwrite(STDERR, "Set MAILCORE_FROM to your approved sender and MAILCORE_TO to the recipient\n"); exit(1); } $context = stream_context_create(['http' => [ 'method' => 'POST', 'header' => "Authorization: Bearer {$apiKey}\r\nIdempotency-Key: {$key}\r\nContent-Type: application/json\r\n", 'content' => json_encode($payload, JSON_THROW_ON_ERROR), 'timeout' => 30, 'ignore_errors' => true, 'follow_location' => 0, // Do not forward the Bearer key on redirects. ], 'ssl' => ['verify_peer' => true, 'verify_peer_name' => true]]); $result = @file_get_contents('https://mailcore.uz/api/email/v1/emails', false, $context, 0, 1048577); if ($result === false) { fwrite(STDERR, "No usable HTTP result. Reconcile with the saved event key and unchanged payload.\n"); exit(1); } $status = 0; foreach ($http_response_header ?? [] as $header) { if (preg_match('/^HTTP\/\S+ (\d{3})/', $header, $match)) $status = (int) $match[1]; } try { $document = json_decode($result, true, 512, JSON_THROW_ON_ERROR); if (!is_array($document)) throw new RuntimeException('Unexpected response'); if ($status !== 202) { fwrite(STDERR, json_encode(['httpStatus' => $status] + $document, JSON_THROW_ON_ERROR) . "\n"); exit(1); } echo json_encode($document, JSON_THROW_ON_ERROR) . "\n"; } catch (Throwable $error) { fwrite(STDERR, "No usable JSON result. Reconcile with the saved event key and unchanged payload.\n"); exit(1); }