Skip to content

Instantly share code, notes, and snippets.

@ozgursar
Created February 1, 2026 09:50
Show Gist options
  • Select an option

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

Select an option

Save ozgursar/ec4997827c49f7348aff6c52704454a1 to your computer and use it in GitHub Desktop.
Email Logger MU Plugin for WordPress Multisite
<?php
/**
* Plugin Name: Network Email Logger
* Description: Logs all emails sent across the network
* Network: true
*/
add_filter('wp_mail', 'network_log_wp_mail', 999);
function network_log_wp_mail($args) {
// Store in network option (available across all sites)
$emails = get_site_option('network_emails_log', array());
$email_data = array(
'time' => current_time('mysql'),
'to' => $args['to'],
'subject' => $args['subject'],
'message' => $args['message'],
'headers' => $args['headers'] ?? '',
'site_id' => get_current_blog_id(),
);
// Keep last 50 emails
array_unshift($emails, $email_data);
$emails = array_slice($emails, 0, 50);
update_site_option('network_emails_log', $emails);
return $args;
}
// Add Network Admin menu to view emails
add_action('network_admin_menu', 'network_email_log_menu');
function network_email_log_menu() {
add_menu_page(
'Email Log',
'Email Log',
'manage_network',
'network-email-log',
'network_email_log_page',
'dashicons-email'
);
}
function network_email_log_page() {
$emails = get_site_option('network_emails_log', array());
echo '<div class="wrap">';
echo '<h1>Network Email Log</h1>';
if (isset($_POST['clear_log'])) {
delete_site_option('network_emails_log');
echo '<div class="notice notice-success"><p>Email log cleared!</p></div>';
$emails = array();
}
echo '<form method="post" style="margin-bottom: 20px;">';
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>Site ID:</strong> ' . esc_html($email['site_id']) . '</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