Skip to content

Instantly share code, notes, and snippets.

@ozgursar
Last active February 1, 2026 09:49
Show Gist options
  • Select an option

  • Save ozgursar/64fd58d2fc5b0aca62487cdc9493e8a4 to your computer and use it in GitHub Desktop.

Select an option

Save ozgursar/64fd58d2fc5b0aca62487cdc9493e8a4 to your computer and use it in GitHub Desktop.
Email Logger MU Plugin for WordPress
<?php
/**
* Plugin Name: Email Logger
* Description: Logs all emails sent from WordPress
*/
add_filter('wp_mail', 'log_wp_mail', 999);
function log_wp_mail($args) {
// Store in regular option (single site)
$emails = get_option('emails_log', array());
$email_data = array(
'time' => current_time('mysql'),
'to' => $args['to'],
'subject' => $args['subject'],
'message' => $args['message'],
'headers' => $args['headers'] ?? '',
);
// Keep last 50 emails
array_unshift($emails, $email_data);
$emails = array_slice($emails, 0, 50);
update_option('emails_log', $emails);
return $args;
}
// Add admin menu to view emails
add_action('admin_menu', 'email_log_menu');
function email_log_menu() {
add_menu_page(
'Email Log',
'Email Log',
'manage_options',
'email-log',
'email_log_page',
'dashicons-email'
);
}
function email_log_page() {
$emails = get_option('emails_log', array());
echo '<div class="wrap">';
echo '<h1>Email Log</h1>';
if (isset($_POST['clear_log'])) {
check_admin_referer('clear_email_log');
delete_option('emails_log');
echo '<div class="notice notice-success"><p>Email log cleared!</p></div>';
$emails = array();
}
echo '<form method="post" style="margin-bottom: 20px;">';
wp_nonce_field('clear_email_log');
echo '<input type="submit" name="clear_log" class="button" value="Clear Log">';
echo '</form>';
if (empty($emails)) {
echo '<p>No emails logged yet.</p>';
} else {
foreach ($emails as $email) {
echo '<div style="border: 1px solid #ccc; padding: 15px; margin-bottom: 20px; background: #f9f9f9;">';
echo '<p><strong>Time:</strong> ' . esc_html($email['time']) . '</p>';
echo '<p><strong>To:</strong> ' . esc_html(is_array($email['to']) ? implode(', ', $email['to']) : $email['to']) . '</p>';
echo '<p><strong>Subject:</strong> ' . esc_html($email['subject']) . '</p>';
echo '<p><strong>Message:</strong></p>';
echo '<pre style="background: white; padding: 10px; overflow-x: auto;">' . esc_html($email['message']) . '</pre>';
echo '</div>';
}
}
echo '</div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment