transport_check.php 4.3 KB

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