Created
February 20, 2026 15:37
-
-
Save bhubbard/6e35a4ea677207928cd53bfe6c75c857 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: WP-CLI Custom Mail Command | |
| * Description: Adds a native `wp mail` command to test and debug wp_mail() deliverability. | |
| * Version: 1.0 | |
| */ | |
| // Exit if WP-CLI is not running | |
| if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { | |
| return; | |
| } | |
| class WP_CLI_Mail_Command { | |
| /** | |
| * Sends an email using the native wp_mail() function. | |
| * | |
| * ## OPTIONS | |
| * | |
| * <to> | |
| * : The email address to send to. | |
| * | |
| * <subject> | |
| * : The subject of the email. | |
| * | |
| * <body> | |
| * : The body content of the email. | |
| * | |
| * [--format=<format>] | |
| * : Set to 'html' to send an HTML email. Defaults to 'text'. | |
| * --- | |
| * default: text | |
| * options: | |
| * - text | |
| * - html | |
| * --- | |
| * | |
| * ## EXAMPLES | |
| * | |
| * wp mail send user@example.com "Update" "Your site is updated." | |
| * wp mail send admin@example.com "Report" "<h1>Report</h1>" --format=html | |
| */ | |
| public function send( $args, $assoc_args ) { | |
| list( $to, $subject, $body ) = $args; | |
| $headers = array(); | |
| if ( 'html' === $assoc_args['format'] ) { | |
| $headers[] = 'Content-Type: text/html; charset=UTF-8'; | |
| } | |
| $sent = wp_mail( $to, $subject, $body, $headers ); | |
| if ( $sent ) { | |
| WP_CLI::success( "Email successfully handed off to the mail server for {$to}." ); | |
| } else { | |
| WP_CLI::error( "wp_mail() returned false. The email failed to send." ); | |
| } | |
| } | |
| /** | |
| * Sends a test email and outputs raw PHPMailer debug logs to the terminal. | |
| * | |
| * ## OPTIONS | |
| * | |
| * <to> | |
| * : The email address to send the test to. | |
| * | |
| * ## EXAMPLES | |
| * | |
| * wp mail test admin@example.com | |
| */ | |
| public function test( $args, $assoc_args ) { | |
| list( $to ) = $args; | |
| WP_CLI::log( WP_CLI::colorize( "%B--- Starting PHPMailer Debug Session ---%n" ) ); | |
| // Hook into PHPMailer to force verbose terminal output | |
| add_action( 'phpmailer_init', function( $phpmailer ) { | |
| $phpmailer->SMTPDebug = 3; // Verbose debug output | |
| $phpmailer->Debugoutput = function( $str, $level ) { | |
| WP_CLI::log( trim( $str ) ); | |
| }; | |
| }, 999 ); | |
| $subject = 'WP-CLI SMTP Test'; | |
| $body = 'If you are reading this, the wp_mail() test was successful.'; | |
| $sent = wp_mail( $to, $subject, $body ); | |
| WP_CLI::log( WP_CLI::colorize( "%B--- End Debug Session ---%n" ) ); | |
| if ( $sent ) { | |
| WP_CLI::success( "Test email completed and returned true." ); | |
| } else { | |
| WP_CLI::error( "Test email failed. Check the debug output above." ); | |
| } | |
| } | |
| /** | |
| * Outputs the current wp_mail() and SMTP configuration status. | |
| * | |
| * ## EXAMPLES | |
| * | |
| * wp mail status | |
| */ | |
| public function status( $args, $assoc_args ) { | |
| global $wp_filter; | |
| WP_CLI::line( WP_CLI::colorize( "%WCurrent Mail Configuration:%n" ) ); | |
| // Check for phpmailer_init hooks (usually indicates an SMTP plugin is active) | |
| $hooks = isset( $wp_filter['phpmailer_init'] ) ? count( $wp_filter['phpmailer_init']->callbacks ) : 0; | |
| if ( $hooks > 0 ) { | |
| WP_CLI::success( "Found {$hooks} hook(s) registered to 'phpmailer_init'. An SMTP plugin is likely active." ); | |
| } else { | |
| WP_CLI::warning( "No hooks found on 'phpmailer_init'. WordPress is likely using default PHP mail()." ); | |
| } | |
| // Output default sender info | |
| $from_email = apply_filters( 'wp_mail_from', get_option( 'admin_email' ) ); | |
| $from_name = apply_filters( 'wp_mail_from_name', 'WordPress' ); | |
| $data = array( | |
| array( 'Setting' => 'From Email', 'Value' => $from_email ), | |
| array( 'Setting' => 'From Name', 'Value' => $from_name ), | |
| ); | |
| \WP_CLI\Utils\format_items( 'table', $data, array( 'Setting', 'Value' ) ); | |
| } | |
| } | |
| // Register the command namespace | |
| WP_CLI::add_command( 'mail', 'WP_CLI_Mail_Command' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment