relay_check.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $relayhost_id = intval($_GET['relayhost_id']);
  7. if (isset($_GET['mail_from']) && filter_var($_GET['mail_from'], FILTER_VALIDATE_EMAIL)) {
  8. $mail_from = $_GET['mail_from'];
  9. }
  10. else {
  11. $mail_from = "relay@example.org";
  12. }
  13. $relayhost_details = relayhost('details', $relayhost_id);
  14. if (!empty($relayhost_details)) {
  15. // Remove [ and ]
  16. $hostname_w_port = preg_replace('/\[|\]/', '', $relayhost_details['hostname']);
  17. // Explode to hostname and port
  18. list($hostname, $port) = explode(':', $hostname_w_port);
  19. // Use port 25 if no port was given
  20. $port = (empty($port)) ? 25 : $port;
  21. $username = $relayhost_details['username'];
  22. $password = $relayhost_details['password'];
  23. $mail = new PHPMailer;
  24. $mail->Timeout = 10;
  25. $mail->SMTPOptions = array(
  26. 'ssl' => array(
  27. 'verify_peer' => false,
  28. 'verify_peer_name' => false,
  29. 'allow_self_signed' => true
  30. )
  31. );
  32. $mail->SMTPDebug = 3;
  33. $mail->Debugoutput = function($str, $level) {
  34. foreach(preg_split("/((\r?\n)|(\r\n?)|\n)/", $str) as $line){
  35. if (empty($line)) { continue; }
  36. if (preg_match("/SERVER \-\> CLIENT: 2\d\d.+/i", $line)) {
  37. echo '<span style="color:darkgreen;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  38. }
  39. elseif (preg_match("/SERVER \-\> CLIENT: 3\d\d.+/i", $line)) {
  40. echo '<span style="color:lightgreen;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  41. }
  42. elseif (preg_match("/SERVER \-\> CLIENT: 4\d\d.+/i", $line)) {
  43. echo '<span style="color:yellow;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  44. }
  45. elseif (preg_match("/SERVER \-\> CLIENT: 5\d\d.+/i", $line)) {
  46. echo '<span style="color:red;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  47. }
  48. elseif (preg_match("/CLIENT \-\> SERVER:.+/i", $line)) {
  49. echo '<span style="color:#999;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  50. }
  51. elseif (preg_match("/^(?!SERVER|CLIENT|Connection:|\)).+$/i", $line)) {
  52. echo '<span>&nbsp;&nbsp;&nbsp;&nbsp;↪ ' . htmlspecialchars($line) . '</span><br>';
  53. }
  54. else {
  55. echo htmlspecialchars($line) . '<br>';
  56. }
  57. }
  58. };
  59. $mail->isSMTP();
  60. $mail->Host = $hostname;
  61. if (!empty($username)) {
  62. $mail->SMTPAuth = true;
  63. $mail->Username = $username;
  64. $mail->Password = $password;
  65. }
  66. $mail->Port = $port;
  67. $mail->setFrom($mail_from, 'Mailer');
  68. $mail->Subject = 'A subject for a SMTP test';
  69. $mail->addAddress($RELAY_TO, 'Joe Null');
  70. $mail->Body = 'This is our test body';
  71. $mail->send();
  72. }
  73. else {
  74. echo "Unknown relayhost.";
  75. }
  76. }
  77. else {
  78. echo "Permission denied.";
  79. }