-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
200 lines (178 loc) · 8.45 KB
/
Copy pathindex.php
File metadata and controls
200 lines (178 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
use PHPMailer\PHPMailer\PHPMailer;
function loadMailerDependencies(): void
{
$autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoload)) {
require_once $autoload;
return;
}
$bundledFiles = [
__DIR__ . '/lib/phpmailer/src/Exception.php',
__DIR__ . '/lib/phpmailer/src/PHPMailer.php',
__DIR__ . '/lib/phpmailer/src/SMTP.php',
];
foreach ($bundledFiles as $file) {
if (!file_exists($file)) {
throw new RuntimeException('PHPMailer dependencies were not found. Install with Composer or restore /lib/phpmailer.');
}
require_once $file;
}
}
function postValue(string $key, string $fallback = '', bool $trimValue = true): string
{
if (!isset($_POST[$key])) {
return $fallback;
}
$value = (string) $_POST[$key];
return $trimValue ? trim($value) : $value;
}
function h(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
function selected(string $value, string $expected): string
{
return $value === $expected ? 'selected' : '';
}
$formData = [
'smtp_server' => '',
'smtp_port' => '587',
'smtp_encryption' => 'tls',
'smtp_debug' => '0',
'smtp_username' => '',
'smtp_password' => '',
'email_from' => '',
'email_to' => '',
];
$message = '';
$messageType = 'error';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['smtp'])) {
foreach (array_keys($formData) as $key) {
$formData[$key] = postValue($key, $formData[$key], $key !== 'smtp_password');
}
try {
loadMailerDependencies();
if ($formData['smtp_server'] === '') {
throw new InvalidArgumentException('SMTP server is required.');
}
$port = filter_var(
$formData['smtp_port'],
FILTER_VALIDATE_INT,
['options' => ['min_range' => 1, 'max_range' => 65535]]
);
if ($port === false) {
throw new InvalidArgumentException('SMTP port must be a number between 1 and 65535.');
}
$encryptionMap = [
'none' => '',
'ssl' => PHPMailer::ENCRYPTION_SMTPS,
'tls' => PHPMailer::ENCRYPTION_STARTTLS,
];
if (!array_key_exists($formData['smtp_encryption'], $encryptionMap)) {
throw new InvalidArgumentException('Encryption must be one of: none, ssl, tls.');
}
$allowedDebugLevels = ['0', '1', '2', '3', '4'];
if (!in_array($formData['smtp_debug'], $allowedDebugLevels, true)) {
throw new InvalidArgumentException('Debug mode must be between 0 and 4.');
}
if (!filter_var($formData['email_from'], FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('The "Email from" address is not valid.');
}
if (!filter_var($formData['email_to'], FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('The "Email to" address is not valid.');
}
$email = new PHPMailer(true);
$email->isSMTP();
$email->Host = $formData['smtp_server'];
$email->Port = (int) $port;
$email->SMTPAuth = true;
$email->SMTPDebug = (int) $formData['smtp_debug'];
$email->SMTPSecure = $encryptionMap[$formData['smtp_encryption']];
$email->Username = $formData['smtp_username'];
$email->Password = $formData['smtp_password'];
$email->setFrom($formData['email_from']);
$email->addAddress($formData['email_to']);
$email->Subject = 'SMTP Test Email';
$email->Body = 'Hooray, your SMTP is working correctly!';
$email->send();
$messageType = 'success';
$message = 'SMTP is working and the test email was sent successfully.';
} catch (Throwable $exception) {
$message = 'SMTP test failed: ' . $exception->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Harrison Ratcliffe">
<meta name="description" content="A simple tool to check your SMTP is working, built in PHP.">
<title>PHP SMTP Checker</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://unpkg.com/purecss@2.1.0/build/pure-min.css">
<link rel="stylesheet" href="https://unpkg.com/purecss@2.1.0/build/grids-responsive-min.css">
</head>
<body>
<main class="container">
<form action="<?= h($_SERVER['PHP_SELF'] ?? 'index.php') ?>" method="post" class="pure-form pure-form-stacked">
<fieldset>
<legend>PHP SMTP Checker</legend>
<?php if ($message !== '') : ?>
<div class="alert alert-<?= h($messageType) ?>" role="alert">
<?= h($message) ?>
</div>
<?php endif; ?>
<div class="pure-g">
<div class="pure-u-1 pure-u-md-1-3">
<label for="smtp_server">SMTP Server</label>
<input id="smtp_server" type="text" name="smtp_server" required placeholder="smtp.server.com" class="pure-u-23-24" value="<?= h($formData['smtp_server']) ?>">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="smtp_port">SMTP Port</label>
<input id="smtp_port" type="number" name="smtp_port" required placeholder="587" min="1" max="65535" class="pure-u-23-24" value="<?= h($formData['smtp_port']) ?>">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="smtp_encryption">Encryption</label>
<select id="smtp_encryption" name="smtp_encryption" required class="pure-input-1-2">
<option value="none" <?= selected($formData['smtp_encryption'], 'none') ?>>None</option>
<option value="ssl" <?= selected($formData['smtp_encryption'], 'ssl') ?>>SSL</option>
<option value="tls" <?= selected($formData['smtp_encryption'], 'tls') ?>>TLS</option>
</select>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="smtp_debug">Debug mode</label>
<select id="smtp_debug" name="smtp_debug" required class="pure-input-1-2">
<option value="0" <?= selected($formData['smtp_debug'], '0') ?>>0 (off)</option>
<option value="1" <?= selected($formData['smtp_debug'], '1') ?>>1 (client)</option>
<option value="2" <?= selected($formData['smtp_debug'], '2') ?>>2 (server)</option>
<option value="3" <?= selected($formData['smtp_debug'], '3') ?>>3 (connection)</option>
<option value="4" <?= selected($formData['smtp_debug'], '4') ?>>4 (low level)</option>
</select>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="smtp_username">SMTP Username</label>
<input id="smtp_username" type="text" name="smtp_username" required class="pure-u-23-24" autocomplete="username" value="<?= h($formData['smtp_username']) ?>">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="smtp_password">SMTP Password</label>
<input id="smtp_password" type="password" name="smtp_password" required class="pure-u-23-24" autocomplete="current-password">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="email_from">Email from</label>
<input id="email_from" type="email" name="email_from" required class="pure-u-23-24" autocomplete="email" value="<?= h($formData['email_from']) ?>">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="email_to">Email to</label>
<input id="email_to" type="email" name="email_to" required class="pure-u-23-24" autocomplete="email" value="<?= h($formData['email_to']) ?>">
</div>
</div>
<button type="submit" name="smtp" class="pure-button pure-button-primary">Send test email</button>
</fieldset>
</form>
</main>
</body>
</html>