logger = $logger; } /** * @param $host * @return string[] */ public function query(string $host) { if (getmxrr($host, $mxRecords, $mxWeights) === false) { $this->logger->debug("no MX records for host <$host> found"); return []; } // Sort MX records by weight $sortedRecords = array_combine($mxRecords, $mxWeights); asort($sortedRecords, SORT_NUMERIC); $mxRecords = array_filter(array_keys($sortedRecords), static fn ($record) => !empty($record)); $this->logger->debug('found ' . count($sortedRecords) . " MX records for host <$host>"); if (empty(($mxRecords))) { return []; } return $this->sanitizedRecords($mxRecords); } private function stripSubdomain(string $domain): string { $labels = explode('.', $domain); $top = count($labels) >= 2 ? array_pop($labels) : ''; $second = array_pop($labels); return $second . '.' . $top; } private function sanitizedRecords(array $mxHosts): array { return array_unique(array_merge($mxHosts, array_map([$this, 'stripSubdomain'], $mxHosts))); } }