137 lines
4.5 KiB
PHP
137 lines
4.5 KiB
PHP
<?php
|
|
|
|
require __DIR__ . '/wp-load.php';
|
|
|
|
define('MAIL_TEST_KEY', 'dev');
|
|
|
|
$smtpLog = '';
|
|
add_action('phpmailer_init', function ($phpmailer) use (&$smtpLog) {
|
|
$phpmailer->SMTPDebug = 2;
|
|
$phpmailer->Debugoutput = function ($str, $level) use (&$smtpLog) {
|
|
$smtpLog .= date('Y-m-d H:i:s') . ' ' . htmlentities(preg_replace('/[\r\n]+/', '', $str), ENT_QUOTES, 'UTF-8') . "<br>\n";
|
|
};
|
|
});
|
|
|
|
$success = '';
|
|
$error = '';
|
|
$err = null;
|
|
add_action('wp_mail_failed', function($e) use (&$err) {
|
|
if (is_wp_error($e)) {
|
|
$err = $e->get_error_message();
|
|
} else {
|
|
$err = 'Unknown wp_mail error';
|
|
}
|
|
});
|
|
|
|
$domain = wp_parse_url(home_url(), PHP_URL_HOST);
|
|
$to = "maksym.krugol@wedos.org";
|
|
$headers = [
|
|
"List-Unsubscribe: <mailto:unsubscribe@{$domain}?subject=unsubscribe>, <https://{$domain}/unsubscribe/{$to}>"
|
|
];
|
|
|
|
$subject = "Service status update and new API keys - " . (new DateTime())->format('d.m.Y');
|
|
$message = '';
|
|
$message .= "Service: host-" . bin2hex(random_bytes(2)) . PHP_EOL;
|
|
$message .= "Status: active" . PHP_EOL;
|
|
$message .= "Service tag: " . bin2hex(random_bytes(6)) . PHP_EOL . PHP_EOL;
|
|
for ($i = 1; $i < 9; $i++) {
|
|
$message .= "API key" . $i . ": " . bin2hex(random_bytes(40)) . PHP_EOL;
|
|
}
|
|
$message .= PHP_EOL . "Thank you for your understanding." . PHP_EOL . PHP_EOL . "Support team US1" . PHP_EOL . (new DateTime())->format('d.m.Y H:i');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$to = isset($_POST['to']) ? trim($_POST['to']) : $to;
|
|
$subject = isset($_POST['subject']) ? trim($_POST['subject']) : $subject;
|
|
$message = isset($_POST['message']) ? trim($_POST['message']) : $message;
|
|
$key = isset($_POST['key']) ? trim($_POST['key']) : '';
|
|
|
|
if (!hash_equals(MAIL_TEST_KEY, $key)) {
|
|
$error = 'Incorrect key.';
|
|
} elseif ($to === '' || !is_email($to)) {
|
|
$error = 'Incorrect recipient address.';
|
|
} elseif ($subject === '') {
|
|
$error = 'Incorrect subject.';
|
|
} else {
|
|
|
|
$sent = wp_mail($to, $subject, $message, $headers);
|
|
if ($sent) {
|
|
$success = "Success";
|
|
} else {
|
|
$error = "Failed | " . ($err ? htmlspecialchars($err, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : 'See PHP/WP error_log for details.');
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="ru">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Test send mail</title>
|
|
<style>
|
|
body {
|
|
margin: 16px;
|
|
padding: 0;
|
|
background-color: #1e1f22;
|
|
color: #bcbec4;
|
|
font-family: Consolas, "DejaVu Sans Mono", "Liberation Mono", Menlo, Monaco, "Courier New", monospace;
|
|
font-size: 14px;
|
|
}
|
|
input, textarea {
|
|
padding: 0 8px;
|
|
width: 282px;
|
|
line-height: 24px;
|
|
background-color: transparent;
|
|
color: #bcbec4;
|
|
border: 1px solid #393b40;
|
|
border-radius: 4px;
|
|
}
|
|
button {
|
|
padding: 8px 12px;
|
|
border: 1px solid #ccc;
|
|
border-radius: 4px;
|
|
background: #f5f5f5;
|
|
cursor: pointer;
|
|
}
|
|
hr {
|
|
border: none;
|
|
height: 1px;
|
|
background-color: #393b40;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Test send mail <?php echo uniqid() ?></h1>
|
|
<form method="post">
|
|
<p>
|
|
<label>
|
|
Recipient:<br>
|
|
<input type="email" name="to" required style="width: 400px;" value="<?php echo $to; ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label>
|
|
Subject:<br>
|
|
<input type="text" name="subject" required style="width: 800px;" value="<?php echo $subject; ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label>
|
|
Message:<br>
|
|
<textarea name="message" rows="10" style="width: 800px;"><?php echo esc_textarea($message); ?></textarea>
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label>
|
|
Key:<br>
|
|
<input type="password" name="key" required style="width: 100px;">
|
|
</label>
|
|
<button type="submit">Send</button>
|
|
</p>
|
|
</form>
|
|
<?php echo $domain ?>
|
|
<?php if ($success): ?><p style="color: green;"><?php echo esc_html($success); ?></p><?php endif; ?>
|
|
<?php if ($error): ?><p style="color: red;"><?php echo esc_html($error); ?></p><?php endif; ?>
|
|
<?php if (!empty($smtpLog)): echo '<hr><h2>SMTP debug log</h2><div>' . $smtpLog . '</div>'; endif; ?>
|
|
</body>
|
|
</html>
|