public
nobgit
read
NobMail
Based on mailcow: dockerized
Languages
Repository composition by tracked source files.
PHP
49%
JavaScript
35%
HTML
9%
CSS
4%
Shell
2%
Python
1%
Lua
0%
Perl
0%
Ruby
0%
SCSS
0%
Create file
Wiki Documentation
Clone
https://nobgit.com/orgs/nobgit/nobmail.git
ssh://[email protected]:2222/orgs/nobgit/nobmail.git
Trace
data/web/inc/ajax/transport_check.php
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
1
<?php
2
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
3
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.inc.php';
4
use PHPMailer\PHPMailer\PHPMailer;
5
use PHPMailer\PHPMailer\SMTP;
6
use PHPMailer\PHPMailer\Exception;
8
error_reporting(0);
9
if (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 = "[email protected]";
17
}
18
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 = "[email protected]";
23
}
24
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)) {
45
$hostname = '[' . $hostname . ']';
46
}
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'];
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> ↪ ' . 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';
139
$mail->addAddress($mail_rcpt, 'Joe Null');
140
$mail->Body = 'This is our test body';
141
$mail->send();
142
}
143
else {
144
echo "Unknown transport.";
145
}
146
}
147
else {
148
echo "Permission denied.";
149
}