| |
| <?php
|
|
|
|
|
| add_action('admin_menu', 'register_all_menus');
|
|
|
| function register_all_menus() {
|
| add_menu_page(
|
| 'Email Log',
|
| 'Email Log',
|
| 'manage_options',
|
| 'smtp-log',
|
| 'render_smtp_log_page',
|
| 'dashicons-email-alt',
|
| 99
|
| );
|
| }
|
|
|
|
|
| function render_smtp_log_page() {
|
| $log_file = WP_CONTENT_DIR . '/email_log.txt';
|
|
|
| if (!file_exists($log_file)) {
|
| echo '<div class="notice notice-warning"><p>No log file found yet.</p></div>';
|
| return;
|
| }
|
|
|
|
|
| $lines = file($log_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
| $lines = array_reverse($lines);
|
|
|
|
|
| $lines = array_slice($lines, 0, 200);
|
| ?>
|
|
|
| <!-- Make the log legible -->
|
| <div class="wrap" style="text-align:center">
|
| <h1>SMTP Email Log</h1>
|
| <p class="description">Showing latest <?php echo count($lines) ?> event(s). Newest first, maximum 200 entries.</p>
|
|
|
| <style>
|
| .smtp-log-table { width: 100%; border-collapse: collapse; margin-top: 12px; }
|
| .smtp-log-table th, .smtp-log-table td {
|
| padding: 6px 10px; border-bottom: 1px solid
|
| font-family: monospace; font-size: 12px; text-align: left;
|
| }
|
| .smtp-log-table th { background:
|
| .smtp-log-table tr:hover { background:
|
| .log-error { color:
|
| .log-success { color:
|
| </style>
|
|
|
| <table class="smtp-log-table">
|
| <thead>
|
| <tr><th>Timestamp</th><th>Message</th></tr>
|
| </thead>
|
| <tbody>
|
| <?php foreach ($lines as $line) :
|
| // Parse [timestamp] message
|
| if (preg_match('/^\[([\d\- :]+)\]\s(.*)$/', $line, $m)) {
|
| $ts = $m[1];
|
| $msg = $m[2];
|
| } else {
|
| $ts = '';
|
| $msg = $line;
|
| }
|
| $class = '';
|
| if (preg_match('/^[45]\d{2}/', $msg)) $class = 'log-error';
|
| elseif (strpos($msg, '250 2.0.0') !== false) $class = 'log-success';
|
| ?>
|
| <tr class="<?= $class ?>">
|
| <td><?= esc_html($ts) ?></td>
|
| <td><?= esc_html($msg) ?></td>
|
| </tr>
|
| <?php endforeach; ?>
|
| </tbody>
|
| </table>
|
| </div>
|
| <?php
|
| }
|
|
|
| // Enable Gmail SMTP
|
| add_action('phpmailer_init', 'enable_gmail_smtp');
|
|
|
| function enable_gmail_smtp($phpmailer) {
|
|
|
| $phpmailer->isSMTP();
|
| $phpmailer->Host = 'smtp.gmail.com';
|
| $phpmailer->SMTPAuth = true;
|
| $phpmailer->Port = 587;
|
| $phpmailer->SMTPSecure = 'tls';
|
| $phpmailer->Username = '[email protected]';
|
| $phpmailer->Password = 'abcd efgh ijkl mnop';
|
| $phpmailer->From = '[email protected]';
|
| $phpmailer->FromName = 'first last';
|
|
|
|
|
|
|
| $phpmailer->SMTPDebug = 2;
|
| $phpmailer->Debugoutput = 'enable_email_log';
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| function enable_email_log($message, $level) {
|
|
|
| if (strpos($message, '250 2.0.0') === false
|
| && !preg_match('/^[45]\d{2}/', $message)
|
| && $level !== 4
|
| ) {
|
| return;
|
| }
|
|
|
| $log_file = WP_CONTENT_DIR . '/email_log.txt';
|
| $timestamp = current_time('mysql');
|
| file_put_contents($log_file, "[$timestamp] $message" . PHP_EOL, FILE_APPEND | LOCK_EX);
|
| }
|
|
|
|
|
|
|
| |
| |
Comments