transport_check.php 5.8 KB

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