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