transport_check.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  3. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.inc.php';
  4. use PHPMailer\PHPMailer\PHPMailer;
  5. use PHPMailer\PHPMailer\SMTP;
  6. use PHPMailer\PHPMailer\Exception;
  7. error_reporting(0);
  8. if (isset($_SESSION['mailcow_cc_role']) && $_SESSION['mailcow_cc_role'] == "admin") {
  9. $transport_id = intval($_GET['transport_id']);
  10. $transport_type = $_GET['transport_type'];
  11. if (isset($_GET['mail_from']) && filter_var($_GET['mail_from'], FILTER_VALIDATE_EMAIL)) {
  12. $mail_from = $_GET['mail_from'];
  13. }
  14. else {
  15. $mail_from = "relay@example.org";
  16. }
  17. if ($transport_type == 'transport-map') {
  18. $transport_details = transport('details', $transport_id);
  19. $nexthop = $transport_details['nexthop'];
  20. }
  21. elseif ($transport_type == 'sender-dependent') {
  22. $transport_details = relayhost('details', $transport_id);
  23. $nexthop = $transport_details['hostname'];
  24. }
  25. if (!empty($transport_details)) {
  26. // Remove [ and ]
  27. $hostname_w_port = preg_replace('/\[|\]/', '', $nexthop);
  28. preg_match('/\[.+\](:.+)/', $nexthop, $hostname_port_match);
  29. preg_match('/\[\d\.\d\.\d\.\d\](:.+)/', $nexthop, $ipv4_port_match);
  30. $has_bracket_and_port = (isset($hostname_port_match[1])) ? true : false;
  31. $is_ipv4_and_has_port = (isset($ipv4_port_match[1])) ? true : false;
  32. $skip_lookup_mx = strpos($nexthop, '[');
  33. // Explode to hostname and port
  34. if ($has_bracket_and_port) {
  35. $port = substr($hostname_w_port, strrpos($hostname_w_port, ':') + 1);
  36. $hostname = preg_replace('/'. preg_quote(':' . $port, '/') . '$/', '', $hostname_w_port);
  37. if (filter_var($hostname, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  38. $hostname = '[' . $hostname . ']:';
  39. }
  40. }
  41. else {
  42. if ($is_ipv4_and_has_port) {
  43. $port = substr($hostname_w_port, strrpos($hostname_w_port, ':') + 1);
  44. $hostname = preg_replace('/'. preg_quote(':' . $port, '/') . '$/', '', $hostname_w_port);
  45. }
  46. if (filter_var($hostname_w_port, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  47. $hostname = $hostname_w_port;
  48. $port = null;
  49. }
  50. elseif (filter_var($hostname_w_port, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  51. $hostname = '[' . $hostname_w_port . ']';
  52. $port = null;
  53. }
  54. else {
  55. $hostname = preg_replace('/'. preg_quote(':' . $port, '/') . '$/', '', $hostname_w_port);
  56. $port = null;
  57. }
  58. }
  59. // Try to get MX if host is not [host]
  60. if ($skip_lookup_mx === false) {
  61. getmxrr($hostname, $mx_records, $mx_weight);
  62. if (!empty($mx_records)) {
  63. for ($i = 0; $i < count($mx_records); $i++) {
  64. $mxs[$mx_records[$i]] = $mx_weight[$i];
  65. }
  66. asort ($mxs);
  67. $records = array_keys($mxs);
  68. echo 'Using first matched primary MX for "' . $hostname . '": ';
  69. $hostname = $records[0];
  70. echo $hostname . '<br>';
  71. }
  72. else {
  73. echo 'No MX records for ' . $hostname . ' were found in DNS, skipping and using hostname as next-hop.<br>';
  74. }
  75. }
  76. // Use port 25 if no port was given
  77. $port = (empty($port)) ? 25 : $port;
  78. $username = $transport_details['username'];
  79. $password = $transport_details['password'];
  80. $mail = new PHPMailer;
  81. $mail->Timeout = 10;
  82. $mail->SMTPOptions = array(
  83. 'ssl' => array(
  84. 'verify_peer' => false,
  85. 'verify_peer_name' => false,
  86. 'allow_self_signed' => true
  87. )
  88. );
  89. $mail->SMTPDebug = 3;
  90. // smtp: and smtp_enforced_tls: do not support wrapped tls, todo?
  91. // change postfix map to detect wrapped tls or add a checkbox to toggle wrapped tls
  92. // if ($port == 465) {
  93. // $mail->SMTPSecure = "ssl";
  94. // }
  95. $mail->Debugoutput = function($str, $level) {
  96. foreach(preg_split("/((\r?\n)|(\r\n?)|\n)/", $str) as $line){
  97. if (empty($line)) { continue; }
  98. if (preg_match("/SERVER \-\> CLIENT: 2\d\d.+/i", $line)) {
  99. echo '<span style="color:darkgreen;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  100. }
  101. elseif (preg_match("/SERVER \-\> CLIENT: 3\d\d.+/i", $line)) {
  102. echo '<span style="color:lightgreen;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  103. }
  104. elseif (preg_match("/SERVER \-\> CLIENT: 4\d\d.+/i", $line)) {
  105. echo '<span style="color:yellow;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  106. }
  107. elseif (preg_match("/SERVER \-\> CLIENT: 5\d\d.+/i", $line)) {
  108. echo '<span style="color:red;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  109. }
  110. elseif (preg_match("/CLIENT \-\> SERVER:.+/i", $line)) {
  111. echo '<span style="color:#999;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  112. }
  113. elseif (preg_match("/^(?!SERVER|CLIENT|Connection:|\)).+$/i", $line)) {
  114. echo '<span>&nbsp;&nbsp;&nbsp;&nbsp;↪ ' . htmlspecialchars($line) . '</span><br>';
  115. }
  116. else {
  117. echo htmlspecialchars($line) . '<br>';
  118. }
  119. }
  120. };
  121. $mail->isSMTP();
  122. $mail->Host = $hostname;
  123. if (!empty($username)) {
  124. $mail->SMTPAuth = true;
  125. $mail->Username = $username;
  126. $mail->Password = $password;
  127. }
  128. $mail->Port = $port;
  129. $mail->setFrom($mail_from, 'Mailer');
  130. $mail->Subject = 'A subject for a SMTP test';
  131. $mail->addAddress($RELAY_TO, 'Joe Null');
  132. $mail->Body = 'This is our test body';
  133. $mail->send();
  134. }
  135. else {
  136. echo "Unknown transport.";
  137. }
  138. }
  139. else {
  140. echo "Permission denied.";
  141. }