#28618 closed defect (bug) (maybelater)
Allow PHPMailer class to be reliably overriden
| Reported by: | leewillis77 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Version: | 4.0 | |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
Description
The WordPress unit test suite currently overrides the global $phpmailer variable with its own MockMailer class. (See https://core.trac.wordpress.org/browser/trunk/tests/phpunit/includes/bootstrap.php#L48)
This achieves the desired effect, but is potentially unreliable.
If the global $phpmailer object gets unset, or overridden (For example by a buggy, or otherwise badly behaving plugin), then the code in wp-includes/pluggable.php will create a new instance of PHPMailer without any way for code to override that - see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/pluggable.php#L264
The end result would be that a site that shouldn't be sending emails will potentially become one that will.
Potential patch attached. This includes the change to pluggable.php to filter the object after creation, and addition to the test suite to use it.
Attachments (1)
Change History (28)
#3
@
6 years ago
- Keywords needs-refresh added
- Milestone → Future Release
28618.diff needs to be refreshed, and the comments above still need to be addressed.
#4
@
6 years ago
- Keywords needs-refresh removed
I'm not clear that the patch does need to address the comments in #1. They are a potential improvement over-and-above this change, not critical to the actual issue (unless I've misunderstood)?
#5
in reply to: ↑ description
@
15 months ago
- Keywords needs-test-info added
Replying to leewillis77:
For example by a buggy, or otherwise badly behaving plugin
@leewillis77 can you provide a demo use case for this? More or less I can see your proposal, but I can see a real practical use without messing too much with the tests code.
I can't really see a case where a plugin can be actually interfering with the test suite (unless it's actually your plugin and you are testing for it).
Also, I don't really get what @nacin is proposing. In conclusion, this needs-test-info to proceed.
I provide a refreshed patch, just in case.
This ticket was mentioned in PR #9403 on WordPress/wordpress-develop by @SirLouen.
15 months ago
#6
- Keywords has-unit-tests added
This is a refresh for 28618.diff
Although more Test Info is needed to proceed with further testing and analysis.
Trac ticket: https://core.trac.wordpress.org/ticket/28618
#7
@
15 months ago
- Keywords close reporter-feedback added; needs-test-info removed
After re-reading and trying to play around with this, I still don't really understand how this can happen from outside the PHPUnit suite.
But if we focus on a Unit Tests perspective, it's true that unsetting the global, brings a real PHP Mailer object instead of keeping it Mock. I've added a Unit Test to demo this.
The idea behind this report is definitely legit, but personally, I don't love this kind of reports that I cannot prove them with a real life case.
Despite personally doing some work on this report and demonstrating the issue with a unit test and refreshing the patch, I'm going to set this to close as for me, it's totally unclear what value is providing. I would rather prefer to close this ticket if no answer is given and maybe if someone happens to find it in the future or the reporter happens to provide some extra info, consider reopening it and further working on reviewing all this with more data at hand.
#8
@
15 months ago
- Keywords needs-patch needs-unit-tests good-first-bug has-test-info added; has-patch has-unit-tests close reporter-feedback removed
- Type enhancement → defect (bug)
Ok, after doing a little more research, yes I could reproduce this in a single test, but I could not reproduce in a plugin as the reporter suggested, because I forgot that tests run in a different DB, and there should not be any active_plugins in the wp_options table by default unless we are doing something very wrong. This plugin thing was misdirecting my attention.
But now I've been looking into how the tear_down in wpMail is running the reset_phpmailer_instance.
The reality is that, if we test this way:
/**
* @group sample
*/
public function test_unsetting_phpmailer_instance() {
global $phpmailer;
$phpmailer = null;
$this->assertNull( $phpmailer );
}
/**
* @group sample
*/
public function test_random_delivery() {
wp_mail( 'test@example.com', 'Test', 'Test' );
global $phpmailer;
$this->assertInstanceOf( 'MockPHPMailer', $phpmailer );
}
The PHPMailer instance is not being completely reset in tear_down in reset_phpmailer_instance and the email is being sent with a regular PHPMailer object.
This section of code is not doing the job:
The problem is that the $mailer conditional is not covering the scenario where $phpmailer is unset.
This was addressed in [41185], but was not sufficiently addressed, not covering this edge case. What I'm trying to devise is why the conditional was introduced first in [37358].
I think that the object should be reset unconditionally. I would like to see a patch proposal.
@SirLouen commented on PR #9403:
15 months ago
#9
This PR is not valid anymore.
This ticket was mentioned in PR #10285 on WordPress/wordpress-develop by @peterhebert.
13 months ago
#10
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
added filter to make it simpler to override the global mailer change tests to use new filter
Trac ticket: https://core.trac.wordpress.org/ticket/28618
This ticket was mentioned in Slack in #core by peterhebert. View the logs.
13 months ago
#12
in reply to: ↑ description
@
9 months ago
- Keywords needs-refresh added
Tested the attached patch (28618.diff) against the latest WordPress trunk.
Environment:
- WordPress: Latest trunk
- PHP: 8.x
- Local environment: XAMPP
- OS: Windows
Result:
The patch does not apply cleanly anymore. Running git apply --check 28618.diff reports conflicts in wp-includes/pluggable.php, and some referenced PHPUnit files no longer exist.
This indicates the patch is outdated and needs a refresh for the current codebase.
This ticket was mentioned in PR #13887 on WordPress/wordpress-develop by @binsaifullah.
6 weeks ago
#13
- Keywords needs-refresh removed
This implements the approach suggested by @SirLouen in comment:8: reset the mock mailer unconditionally.
reset_phpmailer_instance() only replaced the global $phpmailer when it was already truthy. If a test unset the global (or set it to null/false), the helper returned false without doing anything. The next wp_mail() call then created a real WP_PHPMailer, so the test suite could send real email.
Changes
tests/phpunit/includes/mock-mailer.php:reset_phpmailer_instance()now always installs a freshMockPHPMailerand returnstrue. No callers in the suite use the return value.tests/phpunit/tests/pluggable/wpMail.php: new tests (@ticket 28618) cover an unset global,null,falseand a realWP_PHPMailer, plus an end-to-end check thatwp_mail()goes through the mock after the global was unset.
This is a test-suite-only change. It doesn't add the wp_phpmailer filter to core proposed in #10285. That PR now conflicts with trunk, and removing the override in tests/phpunit/includes/install.php would leave the install step without a mock mailer. Whether core should get a filter can be decided on the ticket separately.
Testing (local, MariaDB 13, PHP 8.5)
- New tests against trunk's helper: 4 of 5 fail. Most importantly,
wp_mail()ends up using a realWP_PHPMailerafter the global is unset. - With the fix:
wpMail.php: OK (37 tests, 95 assertions)--group mail: OK (39 tests)- Every file that calls
reset_phpmailer_instance()passes:comment.php,user.php,user/wpSendUserRequest.php, and the threeprivacy/wpPrivacySend*files.
phpcspasses on both changed files.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Analyzing the ticket history, drafting the fix and tests, and running the test suite. I reviewed the final changes and take responsibility for them.
---
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.
🤖 Generated with Claude Code
#14
@
6 weeks ago
Following @SirLouen's suggestion in comment:8, I opened PR 13887. It makes reset_phpmailer_instance() reset the mock unconditionally.
Previously the helper only replaced the global when it was truthy. If a test unset $phpmailer (or set it to null/false), the helper returned false without doing anything, and the next wp_mail() created a real WP_PHPMailer. The helper now always installs a fresh MockPHPMailer.
New tests (@ticket 28618) cover an unset, null, false or real-mailer global, plus an end-to-end wp_mail() check.
Testing (local, MariaDB, PHP 8.5)
- New tests against trunk's helper: 4 of 5 fail. Notably,
wp_mail()uses a realWP_PHPMailerafter the global is unset, which reproduces this ticket. - With the fix:
pluggable/wpMail.phpOK (37 tests),--group mailOK (39 tests). Every file that callsreset_phpmailer_instance()passes (comment, user, privacy). phpcspasses.
This is a test-suite-only change. The core wp_phpmailer filter in PR 10285 could still be discussed separately, but that PR conflicts with trunk now. It also removes the mock mailer override in tests/phpunit/includes/install.php, so the install step would run with a real mailer.
#15
follow-up:
↓ 16
@
6 weeks ago
Hey @binsaifullah without doing a review, which would you tell that are the differences with my patch?
#16
in reply to: ↑ 15
@
6 weeks ago
Thanks @SirLouen! The main differences from PR 9403:
- Scope: PR 9403 adds a new public
wp_phpmailerfilter inwp_mail()(a new API that needs@sinceand probably a dev note) and hooks it in the test bootstrap. PR 13887 only changes the test helper. It doesn't touch core, following your suggestion in comment:8 that the object should be reset unconditionally. - Coverage differs. PR 13887 fixes the leak between tests: every class that calls
reset_phpmailer_instance()inset_up()/tear_down()now always gets a fresh mock. It does not cover your scenario where a single test nulls the global and then callswp_mail()straight away. I ran yourtest_global_override_can_create_real_phpmailer()against my branch, and it still gets a realWP_PHPMailer. Only the core filter can catch that case. - Smaller detail: the filter callback in 9403 returns
new MockPHPMailer()withouttrue. So mocks created that way don't throw exceptions, unlike production (new WP_PHPMailer( true )) andreset_phpmailer_instance(). That could change howwp_mail_failedtests behave.
So the two approaches complement each other. If a core filter is acceptable, it covers more, and the helper change still makes resets reliable. If you'd rather avoid a new API, 13887 is the test-only option. Happy to combine them in one PR, whichever direction you prefer.
#17
@
6 weeks ago
- Keywords good-first-bug removed
@binsaifullah to be clear, I'm not sure what you are trying to accomplish with this
In the tests you unset the phpmailer global but I don't understand what are you trying to prove. Judging by the report, this is not what the user is actually reporting as the issue but actually dealing with the global.
Can you give me a real-life non-hyphotetical case, step-by-step scenario to reproduce this issue you are proposing?
@SirLouen commented on PR #13887:
5 weeks ago
#19
I'm still not sure if this is a better solution than #9403
In fact I'm not even sure if this should be fixed at all sincere there are few real examples of the utility other than not practical edge cases unlikely to happen.
Personally I'm inclined to close everything including #28618 unless @zakariabinsaifullah can give me more insight or an answer in the ticket to my last concerns.
@lancewillett commented on PR #13887:
2 weeks ago
#20
What's next on this one?
@SirLouen commented on PR #13887:
2 weeks ago
#21
What's next on this one?
What do you mean?
@lancewillett commented on PR #13887:
2 weeks ago
#22
What's the next step to either land the change, adjust, or close?
Your comments on https://github.com/WordPress/wordpress-develop/pull/13887#issuecomment-6042546029 is not resolved yet.
@SirLouen commented on PR #13887:
2 weeks ago
#23
What's the next step to either land the change, adjust, or close?
Your comments on #13887 (comment) is not resolved yet.
I'm inclined into closing this. This is the problem of leaving certain tickets open for so long: They have poor context and is unlikely you will get back the OP again on the conversation. I proposed a fix back on the day. Theoretically the problem was that some tests could actually send emails without the right mocking clauses, but I don't think this fix actually fixes this and I don't really see any scenarios where the theory comes true so now it's just hyphotetical.
@lancewillett commented on PR #13887:
2 weeks ago
#24
Closed. We can reopen or retool later.
#25
@
2 weeks ago
- Keywords close added; has-test-info has-patch has-unit-tests 2nd-opinion removed
- Resolution → fixed
- Status new → closed
As I've commented with @lancewillett we are closing this ticket for maybelater as we don't have any test scenarios and OP did not provide such.
@leewillis77 feel free to reopen the ticket if you can provide a real path to trigger this issue (not a hypothetical,non-verifiable scenario).
I don't hate this, so that's good. :) Will want some other feedback.
Another option: Note that this object does basically nothing on construct so we could probably just always construct it and work around it in our tests some other way.