[wp-trac] [WordPress Trac] #63127: wp_mail resets custom headers when headers array is set to empty, causing loss of custom header data (e.g., boundary)

WordPress Trac noreply at wordpress.org
Sun Aug 3 12:54:17 UTC 2025


#63127: wp_mail resets custom headers when headers array is set to empty, causing
loss of custom header data (e.g., boundary)
--------------------------+------------------------------
 Reporter:  regnalf       |       Owner:  (none)
     Type:  defect (bug)  |      Status:  closed
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Mail          |     Version:  6.7.2
 Severity:  normal        |  Resolution:  duplicate
 Keywords:                |     Focuses:
--------------------------+------------------------------
Changes (by SirLouen):

 * status:  new => closed
 * resolution:   => duplicate


Comment:

 Hello @regnalf
 This is not a problem of `wp_mail` but `PHPMailer`
 Long story short: If you pass a `Content-Type` to `PHPMailer` as a custom
 header, without using their functions, it will default into a simple
 `Content-Type` header, that removes the `boundary`.
 This is more detailed in #15448
 ([https://core.trac.wordpress.org/ticket/15448#comment:71 explained in
 detail here])
 There is a workaround, by using `phpmailer_init` hook, and inserting the
 multipart as PHPMailer likes (I'm assuming you are trying to send a
 multipart email, if you are trying to set the `boundary` in the `Content-
 Type`)

 The workaround looks like this:
 {{{#!php
 <?php

 add_action('phpmailer_init','wp_mail_set_text_body');
 function wp_mail_set_text_body($phpmailer) {
      if (empty($phpmailer->AltBody)) {$phpmailer->AltBody =
 wp_strip_all_tags($phpmailer->Body);}
 }
 }}}

 And then you can pass a `$message` to `wp_mail()` with the HTML part, and
 you will see that the multipart with the automatic boundaries will be
 working flawlessly thanks to PHPMailer internals.

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


More information about the wp-trac mailing list