mta-sts.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  3. if (!isset($_SERVER['HTTP_HOST']) || strpos($_SERVER['HTTP_HOST'], 'mta-sts.') !== 0) {
  4. http_response_code(404);
  5. exit;
  6. }
  7. $host = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
  8. $domain = idn_to_ascii(strtolower(str_replace('mta-sts.', '', $host)), 0, INTL_IDNA_VARIANT_UTS46);
  9. // Validate domain or return 404 on error
  10. if ($domain === false || empty($domain)) {
  11. http_response_code(404);
  12. exit;
  13. }
  14. // Check if domain is an alias domain and resolve to target domain
  15. try {
  16. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
  17. $stmt->execute(array(':domain' => $domain));
  18. $alias_row = $stmt->fetch(PDO::FETCH_ASSOC);
  19. if ($alias_row !== false && !empty($alias_row['target_domain'])) {
  20. // This is an alias domain, use the target domain for MTA-STS lookup
  21. $domain = $alias_row['target_domain'];
  22. }
  23. } catch (PDOException $e) {
  24. // On database error, return 404
  25. http_response_code(404);
  26. exit;
  27. }
  28. $mta_sts = mailbox('get', 'mta_sts', $domain);
  29. if (count($mta_sts) == 0 ||
  30. !isset($mta_sts['version']) ||
  31. !isset($mta_sts['mode']) ||
  32. !isset($mta_sts['max_age']) ||
  33. !isset($mta_sts['mx']) ||
  34. $mta_sts['active'] != 1) {
  35. http_response_code(404);
  36. exit;
  37. }
  38. header('Content-Type: text/plain; charset=utf-8');
  39. echo "version: {$mta_sts['version']}\n";
  40. echo "mode: {$mta_sts['mode']}\n";
  41. echo "max_age: {$mta_sts['max_age']}\n";
  42. foreach ($mta_sts['mx'] as $mx) {
  43. echo "mx: {$mx}\n";
  44. }
  45. ?>