Enjoying these plugins? ☕ Buy me a coffee to support ongoing development.

Email Log Viewer

A WordPress plugin that logs all outgoing emails sent by wp_mail() for debugging purposes. Perfect for development, staging, and production environments where email monitoring is essential.

Single File v1.0.0 Updated 1 month ago

YT Email Log Viewer

A WordPress plugin that logs all outgoing emails sent by wp_mail() for debugging purposes. Perfect for development, staging, and production environments where email monitoring is essential.

Features

  • Automatic Email Logging: Captures all emails sent via wp_mail()
  • Custom Database Table: Stores logs efficiently in a dedicated table
  • Admin Interface: Clean, intuitive interface for viewing email logs
  • Status Tracking: Distinguishes between sent and failed emails
  • Detailed View: Expandable rows to view full email content, headers, and messages
  • Pagination: Handles large numbers of logs efficiently (20 per page)
  • Individual Log Deletion: Remove specific logs with AJAX
  • Bulk Clear: Clear all logs with one click
  • Auto-Cleanup: Automatically removes old logs when exceeding 1000 entries
  • Secure: Proper nonce verification, capability checks, and sanitization
  • WPCS Compliant: Follows WordPress Coding Standards
  • Translation Ready: Full i18n/l10n support

Installation

  1. Upload the yt-email-log-viewer folder to /wp-content/plugins/
  2. Activate the plugin through the 'Plugins' menu in WordPress
  3. Access email logs via Tools > Email Logs in the admin menu

Usage

Viewing Email Logs

  1. Navigate to Tools > Email Logs in the WordPress admin
  2. View the list of all logged emails with:
    • ID
    • Status (Sent/Failed)
    • Recipient email address
    • Subject line
    • Date sent
  3. Click View to expand and see full email details:
    • Headers
    • Complete message content

Managing Logs

  • Delete Individual Log: Click the "Delete" button next to any log entry
  • Clear All Logs: Click the "Clear All Logs" button in the header
  • Auto-Cleanup: Plugin automatically maintains a maximum of 1000 logs

Quick Access

  • A "View Logs" link is added to the plugin's row on the Plugins page for quick access

File Structure

yt-email-log-viewer/
├── class-yt-email-log-viewer.php    # Main plugin file (480 lines)
├── assets/
│   ├── css/
│   │   └── admin.css                # Admin interface styles
│   └── js/
│       └── admin.js                 # Admin AJAX functionality
└── README.md                        # This file

Technical Details

Database Schema

The plugin creates a custom table wp_yt_email_logs with the following structure:

CREATE TABLE wp_yt_email_logs (
  id bigint(20) NOT NULL AUTO_INCREMENT,
  email_to varchar(255) NOT NULL,
  subject varchar(255) NOT NULL,
  message longtext NOT NULL,
  headers text,
  status varchar(20) NOT NULL DEFAULT 'sent',
  sent_date datetime NOT NULL,
  PRIMARY KEY (id),
  KEY status (status),
  KEY sent_date (sent_date)
);

Hooks Used

Actions:

  • wp_mail - Captures outgoing email data
  • admin_menu - Adds admin page under Tools
  • admin_enqueue_scripts - Loads CSS/JS on admin page
  • wp_ajax_ytelv_clear_logs - AJAX handler for clearing logs
  • wp_ajax_ytelv_delete_log - AJAX handler for deleting single log

Filters:

  • wp_mail_failed - Captures failed email attempts
  • plugin_action_links_* - Adds "View Logs" link to plugins page

Prefixes

All functions, classes, and database elements are prefixed with ytelv_ or YT_Email_Log_Viewer to avoid conflicts:

  • Class: YT_Email_Log_Viewer
  • Constants: YT_EMAIL_LOG_VIEWER_*
  • Functions: ytelv_*
  • Database Table: wp_yt_email_logs
  • AJAX Actions: ytelv_*
  • CSS/JS Classes: .ytelv-*

Security Features

  • Nonce verification for all AJAX requests
  • Capability checks (manage_options) for admin access
  • Sanitized input data (sanitize_text_field())
  • Escaped output (esc_html(), esc_attr(), esc_url())
  • Prepared SQL statements ($wpdb->prepare())
  • Direct file access prevention

Data Management

On Activation

  • Creates custom database table with proper indexes
  • Uses dbDelta() for safe table creation/updates

On Deactivation

  • Keeps data intact (logs preserved)

On Uninstall

  • Drops custom database table
  • Removes all plugin options
  • Clears cache

Auto-Cleanup

  • Automatically removes oldest logs when exceeding 1000 entries
  • Maintains optimal database performance

Browser Compatibility

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Edge (latest)

WordPress Compatibility

  • Requires WordPress: 5.8 or higher
  • Tested up to: 6.7
  • Requires PHP: 7.4 or higher

Performance

  • Lightweight: Single PHP file + CSS + JS
  • Efficient database queries with proper indexing
  • AJAX-powered interface (no page reloads for actions)
  • Pagination for large datasets
  • Auto-cleanup prevents database bloat

Development

Line Count

  • Main PHP: ~480 lines (including comments)
  • CSS: ~220 lines
  • JS: ~180 lines
  • Total: ~880 lines

Coding Standards

Built following:

  • WordPress Plugin Handbook
  • WordPress Coding Standards (WPCS)
  • PHP 7.4+ best practices

Testing Checklist

  • [x] Plugin activates without errors
  • [x] Database table created successfully
  • [x] Email logging works for sent emails
  • [x] Failed email logging works
  • [x] Admin page displays correctly
  • [x] View details functionality works
  • [x] Delete single log works (AJAX)
  • [x] Clear all logs works (AJAX)
  • [x] Pagination works correctly
  • [x] Auto-cleanup maintains max logs
  • [x] Plugin deactivates cleanly
  • [x] Plugin uninstalls and removes all data
  • [x] No PHP warnings or notices
  • [x] WPCS compliant

Use Cases

  1. Development: Debug email sending issues during development
  2. Staging: Verify email content before production deployment
  3. Production: Monitor outgoing emails and track delivery
  4. Troubleshooting: Investigate email-related issues
  5. Compliance: Maintain records of sent emails
  6. Testing: Verify email functionality without actual sending

Limitations

  • Does not prevent emails from being sent (logs only)
  • Does not handle attachments separately (stored in message body if inline)
  • Maximum 1000 logs stored (auto-cleanup removes older entries)
  • Admin-only access (requires manage_options capability)

Future Enhancements

Potential features for future versions:

  • Export logs to CSV/JSON
  • Email search and filtering
  • Attachments tracking
  • Email resend functionality
  • Scheduled log cleanup options
  • Email statistics dashboard
  • Custom retention periods
  • Role-based access control

Support

For issues, questions, or contributions:

License

GPL v2 or later - https://www.gnu.org/licenses/gpl-2.0.html

Credits

Built using the YT WordPress Plugin Boilerplate following WordPress Plugin Handbook and WPCS guidelines.

Changelog

1.0.0

  • Initial release
  • Email logging functionality
  • Admin interface with pagination
  • Individual log deletion
  • Bulk log clearing
  • Auto-cleanup feature
  • Full WPCS compliance