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