[wp-trac] [WordPress Trac] #33972: static use of PHPMailer class results in stale state between calls to wp_mail()

WordPress Trac noreply at wordpress.org
Mon Sep 8 17:32:27 UTC 2025


#33972: static use of PHPMailer class results in stale state between calls to
wp_mail()
-------------------------------------------------+-------------------------
 Reporter:  codebuddy                            |       Owner:  rishabhwp
     Type:  defect (bug)                         |      Status:  assigned
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  Mail                                 |     Version:  4.3
 Severity:  critical                             |  Resolution:
 Keywords:  has-test-info good-first-bug has-    |     Focuses:
  patch needs-testing                            |
-------------------------------------------------+-------------------------

Comment (by sajjad67):

 = Failed Test Report =

 When testing email sending in WordPress with small email bodies (length <
 1000 characters) using MailPit/MailHog, email sending fails with the
 error:

 {{{
 PHPMailer Error: Unknown encoding:
 }}}

 This occurs after applying the patch from [PR
 #9417](https://github.com/WordPress/wordpress-develop/pull/9417).

 == Environment ==

 * **WordPress:** 6.9-alpha-60093-src
 * **PHP:** 8.2.29
 * **Server:** nginx/1.29.1
 * **Database:** MySQL 8.4.6 (mysqlnd 8.2.29)
 * **Browser:** Chrome 139.0.0.0
 * **OS:** Windows 10/11
 * **Theme:** Twenty Twenty-Five 1.3
 * **Plugins Active:**
   * Email Testing 1.0.0 (custom test plugin)
   * Test Reports 1.2.0

 == Test Plugin Used ==

 **Email Testing Plugin** (custom):
 {{{
   add_action( 'phpmailer_init', 'configure_mailhog_for_testing' );

   function configure_mailhog_for_testing( $phpmailer ) {
     $phpmailer->isSMTP();
     $phpmailer->SMTPAuth    = false;
     $phpmailer->SMTPSecure  = '';
     $phpmailer->SMTPAutoTLS = false;
     $phpmailer->Host        = 'mailpit';
     $phpmailer->Port        = '1025';
   }

   add_action( 'wp_mail_failed', 'log_mailer_errors' );

   function log_mailer_errors( $wp_error ) {
     if ( is_wp_error( $wp_error ) ) {
       error_log( 'PHPMailer Error: ' . $wp_error->get_error_message() );
     }
   }

   add_filter( 'wp_mail_from', 'my_custom_mail_from' );
   function my_custom_mail_from( $email ) {
     return 'from at example.com';
   }

   add_action( 'admin_menu', 'mail_test_menu' );

   function mail_test_menu() {
     add_management_page(
       'Mail Test',
       'Mail Test',
       'manage_options',
       'mail-test',
       'mail_test_page'
     );
   }

   function mail_test_page() {
     ?>
     <div class="wrap">
       <h1>MailHog Email Test</h1>

       <?php
         if ( isset( $_POST['send_test'] ) && check_admin_referer(
 'mail_test_email' ) ) {
           $to = 'test at example.com';
           $subject = "Test Subject";
           $body = 'Small Body'; // it doesn't work.
           // $body = str_repeat( 'A', 1000 ); // it works only, body
 length greater or equal to 1000 works only.

           for ( $i = 1; $i <= 1005; $i++ ) {
             $body = str_repeat( 'A', $i );

             $sent = wp_mail( $to, $subject, $body );

             if ( $sent ) {
               ?>
                 <div class="notice notice-success">
                   <p>Test email sent successfully! Length: <?php echo
 strlen( $body ); ?></p>
                 </div>
               <?php
             } else {
               ?>
                 <div class="notice notice-error">
                   <p>Error sending email, Body : Length: <?php echo
 strlen( $body ); ?></p>
                 </div>
               <?php
             }
           }
         }
       ?>

       <form method="post">
         <?php wp_nonce_field( 'mail_test_email' ); ?>
         <p class="submit">
           <input type="submit" name="send_test" class="button button-
 primary" value="Send Test Email">
         </p>
       </form>
     </div>
     <?php
   }
 }}}

 == ✅ Expected Result ==

 * All test emails should be sent successfully, regardless of body length.

 == ❌ Actual Result ==

 * Emails with **body length < 1000 characters** fail to send.
 * Error logged:

   {{{
   PHPMailer Error: Unknown encoding:
   }}}
 * Emails with **body length ≥ 1000 characters** succeed and are received
 in MailPit.

 📸 **Screenshot:**
 [[Image(https://img001.prntscr.com/file/img001/G-3eCpaoSO6Y7ls3AyuEaQ.png)]]

 == Steps to Reproduce ==

 1. Install and activate the provided **Email Testing** plugin.
 2. Navigate to **Tools → Mail Test** in wp-admin.
 3. Click **Send Test Email**.
 4. Observe results:
   * Messages with body length `< 1000` fail.
   * Messages with body length `≥ 1000` succeed.

 == Notes ==

 * Issue persists after applying [PR #9417](https://github.com/WordPress
 /wordpress-develop/pull/9417).
 * Suggests underlying issue with **body encoding/handling in PHPMailer
 integration** when body length is below threshold.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/33972#comment:22>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list