relay_check.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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->SMTPDebug = 3;
  25. $mail->Debugoutput = function($str, $level) {
  26. foreach(preg_split("/((\r?\n)|(\r\n?)|\n)/", $str) as $line){
  27. if (empty($line)) { continue; }
  28. if (preg_match("/SERVER \-\> CLIENT: 2\d\d.+/i", $line)) {
  29. echo '<span style="color:darkgreen;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  30. }
  31. elseif (preg_match("/SERVER \-\> CLIENT: 3\d\d.+/i", $line)) {
  32. echo '<span style="color:lightgreen;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  33. }
  34. elseif (preg_match("/SERVER \-\> CLIENT: 4\d\d.+/i", $line)) {
  35. echo '<span style="color:yellow;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  36. }
  37. elseif (preg_match("/SERVER \-\> CLIENT: 5\d\d.+/i", $line)) {
  38. echo '<span style="color:red;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  39. }
  40. elseif (preg_match("/CLIENT \-\> SERVER:.+/i", $line)) {
  41. echo '<span style="color:#999;font-weight:bold">' . htmlspecialchars($line) . '</span><br>';
  42. }
  43. elseif (preg_match("/^(?!SERVER|CLIENT|Connection:|\)).+$/i", $line)) {
  44. echo '<span>&nbsp;&nbsp;&nbsp;&nbsp;↪ ' . htmlspecialchars($line) . '</span><br>';
  45. }
  46. else {
  47. echo htmlspecialchars($line) . '<br>';
  48. }
  49. }
  50. };
  51. $mail->isSMTP();
  52. $mail->Host = $hostname;
  53. if (!empty($username)) {
  54. $mail->SMTPAuth = true;
  55. $mail->Username = $username;
  56. $mail->Password = $password;
  57. $mail->Port = $port;
  58. }
  59. $mail->setFrom($mail_from, 'Mailer');
  60. $mail->Subject = 'A subject for a SMTP test';
  61. $mail->addAddress($RELAY_TO, 'Joe Null');
  62. $mail->Body = 'This is our test body';
  63. $mail->send();
  64. }
  65. else {
  66. echo "Unknown relayhost.";
  67. }
  68. }
  69. else {
  70. echo "Permission denied.";
  71. }