Make WordPress Core

Opened 6 months ago

Closed 11 hours ago

#65415 closed defect (bug) (wontfix)

WordPress 7.0 inclusion of PHPMailer 7.0.2 breaks .user.ini override of sendmail_from

Reported by: gmariani405 Owned by:
Priority: normal Milestone:
Component: Mail Version: 7.0
Severity: critical Keywords: close
Cc: Focuses:

Description

I have created a ticket on PHPMailer here: ​https://github.com/PHPMailer/PHPMailer/issues/3342

But the gist is, the recent patch in PHPMailer 7.0.2 prevents the .user.ini from overriding an existing -f flag in the sendmail command. Which prevents sites from fixing the incorrect domain on certain hosts (like Nexcess/LiquidWeb) and causing all mail to no longer be deliverable. I have a workaround for now with this simple patch, but ideally PHPMailer needs to be fixed in order to allow the flexibility to override the default sendmail command.

Here is my workaround:

<?php
add_action('phpmailer_init', function ($phpmailer) {
    $phpmailer->isSendmail();
    $sendmail_from_value = ini_get('sendmail_from');
    if (empty($sendmail_from_value)) return;
    $phpmailer->Sender = $sendmail_from_value;
}, 999);

Attachments (4)

65415-1-mailpit-trunk-return-path-host.png​ (347.5 KB ) - added by sirakashaman 6 weeks ago.
Mailpit: trunk (PHPMailer 7.1.1), Return-Path is the host's address
65415-2-mailpit-phpmailer-7.0.0-return-path-site.png​ (338.8 KB ) - added by sirakashaman 6 weeks ago.
Mailpit: PHPMailer 7.0.0 (as in 6.9.x), Return-Path is the site's address
65415-3-debugger-line-2118-condition-false.png​ (776.8 KB ) - added by sirakashaman 6 weeks ago.
Xdebug: PHPMailer.php line 2118, the condition is false and $params stays null
65415-4-debugger-step-skips-line-2119.png​ (777.5 KB ) - added by sirakashaman 6 weeks ago.
Xdebug: stepping from line 2118 skips line 2119

Change History (19)

#1 @abdullahramzan
6 months ago

  • Keywords dev-feedback 2nd-opinion added

This ticket was mentioned in ​Slack in #core by abdullahramzan. ​View the logs.


6 months ago

#3 @jorbin
5 months ago

  • Milestone Awaiting Review → 7.0.1

Putting in the 7.0.1 milestone for tracking, but fixing this will be dependent on an upstream release

#4 @masteradhoc
5 months ago

  • Milestone 7.0.1 → 7.0.2

As the RC for WP 7.0.1 is expected to land tomorrow we'll have to punt this to 7.0.2.

#5 @jorbin
4 months ago

  • Milestone 7.0.2 → 7.0.3

#6 @mirmpro
4 months ago

Test Report

Description

Tested the issue where setting sendmail_from via ini_set() or configuration settings (such as .user.ini) is ignored by PHPMailer in WordPress 7.0+, leaving the PHPMailer $Sender property unpopulated.

Environment

  • WordPress: 7.1-beta3
  • Subdirectory: No
  • PHP: 8.4.16
  • Server: nginx/1.26.1
  • Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.4.16)
  • Browser: Chrome 150.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.3.0
    • WordPress Beta Tester 4.0.0

Steps taken

  1. Configured sendmail_from in wp-config.php using ini_set('sendmail_from', 'my-email@gmail.com');.
  2. Added a logging hook to phpmailer_init to record both ini_get('sendmail_from') and $phpmailer->Sender.
  3. Triggered a password reset email via the WordPress login screen.
  4. Checked the generated debug log to verify properties.
  5. Issue confirmed: sendmail_from correctly captures the configured email address, but the PHPMailer Sender property remains empty:
 [25-Jul-2026 11:07:29 UTC] PHP sendmail_from: my-email@gmail.com
 [25-Jul-2026 11:07:29 UTC] PHPMailer Sender property:

Expected result

  • When sendmail_from is defined in PHP settings, PHPMailer should automatically populate its $Sender property with that value so outgoing mail correctly reflects the configured envelope sender.

Actual Results

  • Reproduced: ini_get('sendmail_from') returns the correct address, but PHPMailer leaves $phpmailer->Sender unpopulated.

Additional Notes

  • This issue prevents certain hosting environments from overriding default sendmail parameters correctly via .user.ini.

Screenshots/Screencast with results

  • N/A

Support Content

  • N/A

#7 @masteradhoc
3 months ago

  • Milestone 7.0.3 → 7.2

Since the issue remains unresolved upstream and no WordPress 7.0.3 release is currently planned, I'm moving this to 7.2 for consideration.

@sirakashaman
6 weeks ago

Mailpit: trunk (PHPMailer 7.1.1), Return-Path is the host's address

@sirakashaman
6 weeks ago

Mailpit: PHPMailer 7.0.0 (as in 6.9.x), Return-Path is the site's address

@sirakashaman
6 weeks ago

Xdebug: PHPMailer.php line 2118, the condition is false and $params stays null

#8 follow-up: @sirakashaman
6 weeks ago

Hi, thanks for the detailed report and the workaround. I was able to reproduce this on trunk, and as far as I can tell it affects every WordPress 7.0.x and 7.1.x release. Below are my reproduction, what I believe causes it, and two possible fixes that I tested locally. I'd really appreciate feedback on which direction makes sense.

In short

Every email has an envelope sender (shown as Return-Path). Receiving mail servers check that it belongs to the sender's domain; when it doesn't, the email can be rejected or marked as spam.

On some hosts, the server is set up to always send with the host's own address (for example postmaster@random.nxcli.io). Site owners fix this by setting sendmail_from in .user.ini, and up to WordPress 6.9 their emails then went out with the site's address.

Since WordPress 7.0, that setting is ignored on these hosts, so emails go out with the host's address again.

Environment

  • WordPress: 7.2-alpha-63166-src (trunk @ da764706f), bundled PHPMailer 7.1.1
  • PHP: 8.5.3
  • Server: nginx 1.26.1, PHP-FPM
  • Database: MySQL 8.4.0
  • Mail: Mailpit 1.24.1 as the sendmail program. Like the qmail setup in the ticket description, it uses the last -f it receives.

Steps to reproduce

  1. In the server's PHP configuration (PHP-FPM pool or php.ini, which sites can't change), add a -f with a host address:
    sendmail_path = "/path/to/sendmail -t -i -f postmaster@host.example"
    
  2. In the site's .user.ini:
    sendmail_from = me@site.example
    
  3. Trigger any WordPress email, for example Lost your password? on the login screen.
  4. Check the Return-Path of the email that was sent.

Results

WordPress code Bundled PHPMailer Return-Path Expected
trunk 7.1.1 postmaster@host.example ❌ me@site.example
trunk, with only wp-includes/PHPMailer/PHPMailer.php replaced by the copy from 6.9.4 7.0.0 me@site.example ✅ me@site.example

Only the PHPMailer file differs between the two runs, so I believe the change in behaviour comes from the PHPMailer update.

Trunk (PHPMailer 7.1.1), the host's address is used:

Mailpit: trunk (PHPMailer 7.1.1), Return-Path is the host's address

The same email with PHPMailer 7.0.0, as bundled in 6.9.x, uses the site's address:

Mailpit: PHPMailer 7.0.0 (as in 6.9.x), Return-Path is the site's address

The sendmail command that PHPMailer runs, recorded with a stand-in program:

Bundled PHPMailer Command
7.0.0 (WordPress 6.9.x) sendmail -t -i -f postmaster@host.example -fme@site.example ✅
7.0.2 (WordPress 7.0.x) sendmail -t -i -f postmaster@host.example ❌
7.1.1 (WordPress 7.1.x, trunk) sendmail -t -i -f postmaster@host.example ❌

Affected versions

WordPress Bundled PHPMailer Affected
6.9.0 – 6.9.10 7.0.0 No
7.0.0 – 7.0.7 7.0.2 Yes
7.1.0 – 7.1.3 7.1.1 Yes
trunk 7.1.1 Yes

What happens, step by step

As far as I understand the code:

  1. wp_mail() sends with PHP's mail() (pluggable.php line 511) and does not set an envelope sender itself (setFrom( $from_email, $from_name, false ), line 455).
  2. PHPMailer copies sendmail_from into its Sender property (PHPMailer.php line 2114).
  3. PHPMailer then adds -f with that address only if the server's sendmail_path does not already contain -f (line 2118 on trunk, line 2114 on 7.0.x):
    <?php
    if (self::isShellSafe($this->Sender) && strpos($phpmailer_path, ' -f') === false) {
        $params = sprintf('-f%s', $this->Sender);
    }
    
  4. On these hosts it does contain -f, so $params stays null and the site's address is never passed on.

Debugger paused at line 2118 during a Lost Password request. In the Watch panel, Sender is me@site.example and isShellSafe() is true, but the host's -f postmaster@host.example is found in sendmail_path, so strpos(...) === false is false, the whole condition is false, and $params stays null:

Xdebug: PHPMailer.php line 2118, the condition is false and $params stays null

One step later, execution goes from line 2118 straight to line 2121 (highlighted). Line 2119 (outlined), which would add the site's -f, is skipped:

Xdebug: stepping from line 2118 skips line 2119

From what I could find, this check was added in PHPMailer 7.0.2 and came into core with the PHPMailer update. I believe it was meant to prevent a duplicate -f after WordPress started setting a sender by default, which caused "Could not instantiate mail function" on hosts whose sendmail rejects two -f options. WordPress later reverted that change, but the PHPMailer check remains.

Additional findings

  1. A sender set by a plugin is ignored too. With the same server setup and no .user.ini, a plugin that sets $phpmailer->Sender = 'bounce@site.example' in phpmailer_init gets -fbounce@site.example with PHPMailer 7.0.0, but only the host's -f with 7.0.2 and 7.1.1.
  2. Hosts without -f in sendmail_path are not affected. sendmail_from still results in -fme@site.example there.
  3. The workaround from the description works on trunk. Switching to isSendmail() and setting Sender results in sendmail -fme@site.example -i -t.

Proposal

I tried two possible fixes locally. For each one, I stepped through a Lost Password request with Xdebug and checked the Return-Path in Mailpit. Both gave me@site.example. I also ran the other cases (no host -f, no site sender, the same address on both sides), and those behaved as they do on trunk today.

Option 1: fix it in PHPMailer, then update the bundled library in core

In mailSend(), PHPMailer would skip its own -f only when sendmail_path already passes the same address, instead of skipping it whenever there is any -f:

-f in sendmail_path Sender set by the site Command
none me@site.example -fme@site.example (unchanged)
postmaster@host.example not set no extra -f (unchanged)
postmaster@host.example postmaster@host.example no extra -f (no duplicate)
postmaster@host.example me@site.example -f postmaster@host.example -fme@site.example (as in 6.9)
  • I believe this is the cleanest place for the fix, since the change in behaviour came from PHPMailer. Upstream issue: ​https://github.com/PHPMailer/PHPMailer/issues/3342
  • As far as I understand, on hosts whose sendmail rejects two -f options, a site that sets a different address would get "Could not instantiate mail function" again, as it did in 6.9 and earlier. Since changeset 61537, WordPress no longer sets a sender by default, so I think this would only affect sites that set one themselves.
  • It depends on a new PHPMailer release, and the timing may be tight for 7.2.

Option 2: handle it in core, in WP_PHPMailer

WP_PHPMailer::preSend() would check, before the message is built, whether sendmail_path already contains -f and the site has set a different sender (through sendmail_from or Sender). Only in that case, it would switch to isSendmail() and keep the site's sender. This is what the workaround in the description does, done by core. The resulting command has a single -f:

/path/to/sendmail -fme@site.example -i -t
  • It doesn't change the bundled PHPMailer files, and it could be removed once PHPMailer handles this itself.
  • With only one -f, I believe it also works on hosts whose sendmail rejects two -f options.
  • From what I saw while debugging, the switch has to happen before parent::preSend(). When sending with mail(), PHPMailer leaves the To and Subject headers out of the message it builds (mail() adds them). Switching later, in mailSend(), would send an email without them.
  • For the affected sites only, emails would be sent by PHPMailer running the program directly instead of through PHP's mail(), so PHP settings such as mail.log and mail.add_x_header would not apply to them. It would be skipped when popen() is disabled, or when sendmail_path doesn't contain sendmail (for example ssmtp or msmtp), so those hosts would stay as they are today.
  • As I understand comment 3, the fix was expected to come with an upstream release, so I'd only suggest this if a core-side change is acceptable: https://core.trac.wordpress.org/ticket/65415#comment:3

Feedback

I'd appreciate guidance on which direction is preferred:

  1. Option 1 only: I'd propose the change upstream with tests, and core would pick it up with a PHPMailer update.
  2. Option 2 for 7.2: I can attach a patch with unit tests here.
  3. Both: option 2 in core for now, and option 1 upstream so the core code can be removed later.

Please let me know if I've misunderstood anything, or if you'd like me to test other setups.

#9 in reply to: ↑ 8 @siliconforks
6 weeks ago

Replying to sirakashaman:

I'd appreciate guidance on which direction is preferred:

  1. Option 1 only: I'd propose the change upstream with tests, and core would pick it up with a PHPMailer update.
  2. Option 2 for 7.2: I can attach a patch with unit tests here.
  3. Both: option 2 in core for now, and option 1 upstream so the core code can be removed later.

Personally I much prefer Option 1.

Option 2 sounds like it might not be safe for some server configurations.

#10 follow-up: @SirLouen
6 weeks ago

  • Keywords close added; dev-feedback 2nd-opinion removed

As ​I explain here I'm more inclined to close this. The fix that @gmariani405 provided using the phphmailer_init hook with the isSendmail method is the perfect way to fix this edge case.

Proposing close for the 7.2 review.

What do you think, @siliconforks ?

#11 in reply to: ↑ 10 @siliconforks
6 weeks ago

Replying to SirLouen:

Proposing close for the 7.2 review.

What do you think, @siliconforks ?

I certainly don't think there should be any changes in WordPress regarding this issue. So I'm fine with closing this.

For upstream PHPMailer - it's basically a question of whether PHPMailer wants to:

  1. Forbid multiple -f options (the current behavior)
  2. Allow multiple -f options (basically reverting ​https://github.com/PHPMailer/PHPMailer/pull/3283 entirely)
  3. Allow multiple -f options in some cases but not others (this sounds like what @sirakashaman is proposing in "Option 1" above). That might be a viable approach - but that's something that would have to be decided upstream in PHPMailer.

This ticket was mentioned in ​Slack in #core by im3dabasia1. ​View the logs.


5 days ago

#13 @im3dabasia1
5 days ago

As per the pre-RC1 7.2 bug scrub: the discussion leans towards closing, since a workaround exists. @gmariani405, any objection?

#14 @gmariani405
5 days ago

After reading both the WordPress ticket and the PHPMailer ticket, both seem in favor of just using my workaround as the official solution. Can't say that was what I was hoping for since this change broke all the sites I host (and most likely any site on nexcess/liquidweb at the time). But i realize that's still considered an edge-case in the scheme of things. I'm not familiar enough with PHPMailer to have a stronger opinion here so I'll defer to the teams judgement here. Thanks for considering my request.

#15 @audrasjb
11 hours ago

  • Milestone 7.2
  • Resolution → wontfix
  • Status new → closed

Closing as wontfix, as per above comments. Feel free to reopen the ticket if needed.

Note: See TracTickets for help on using tickets.