Problem/Motivation
Error messages popup after some custom mail calls.
as @_renify_ shows below:
Error: Call to a member function has() on null in Drupal\Core\Mail\Plugin\Mail\PhpMail->mail() (line 114 of core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php).
Drupal\mailsystem\Adapter->mail(Array) (Line: 307)
Drupal\Core\Mail\MailManager->doMail('reroute_email', 'test_email_form', 'reniersediego@prometsource.com', 'en', Array, NULL, 1) (Line: 180)
Drupal\Core\Mail\MailManager->Drupal\Core\Mail\{closure}() (Line: 583)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 181)
Drupal\Core\Mail\MailManager->mail('reroute_email', 'test_email_form', 'reniersediego@prometsource.com', 'en', Array, NULL, 1) (Line: 70)
Drupal\mailsystem\MailsystemManager->mail('reroute_email', 'test_email_form', 'reniersediego@prometsource.com', 'en', Array) (Line: 191)
marquettelaw_custom_form_alter(Array, Object, 'reroute_email_test_email_form') (Line: 545)
Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object, 'reroute_email_test_email_form') (Line: 840)
Drupal\Core\Form\FormBuilder->prepareForm('reroute_email_test_email_form', Array, Object) (Line: 284)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 583)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 166)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 74)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)Steps to reproduce
Create a clean install using:
- Drupal: 10.4.7
- Moderation Note: 8.x-1.0-beta6
- set basic page content type to being moderated
- create some test content
- view the node, select the text and try to save the note
| Comment | File | Size | Author |
|---|---|---|---|
| #19 | 2025-05-09_09-59.png | 245.8 KB | igorgoncalves |
| #2 | 3373074-core-10.1.x-1.patch | 895 bytes | _renify_ |
Issue fork drupal-3373074
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3373074-call-to-a
changes, plain diff MR !4338
Comments
Comment #2
_renify_ commented$this->request seems has null value.
Comment #4
tr commentedI don't see how this can possibly do anything. The constructor already sets
$this->request = \Drupal::request();Your patch is basically a regression of some of the work done in #3226117: Uncaught RfcComplianceException when email From name contains a comma.
Comment #5
_renify_ commented$this->request->server has null value even if its already on constructor. Seems wierd behaviour on my end.
Comment #6
swatichouhan012 commentedHIi _renify_
To change the services with Global Service not good practice , i tried to replicate using a custom call but not got getting the mentioned error, would be great if you an provide more info the process you followed.
Thanks
Comment #7
linkanp commented#2 Patch worked for me. Thanks
Comment #8
tr commented@linkanp: Please provide directions for reproducing this error.
The steps in the issue summary do not cause an error for me or for the poster in #6.
Comment #9
joelpittet@TR I'll try to boil down the steps to reproduce but currently this is happening using the following modules:
To reproduce it we have an error in our code and it is going to email it to us with monolog -> mimemail
Callstack:
Comment #10
smustgrave commentedDon't think there is anything to review, Will need a test case showing this issue in core though.
Also don't think we should be undoing the dependency injection.
Comment #11
joelpittetI tried to set a breakpoint in the constructor, but it never hit that breakpoint there for some reason... which explain why it's not set, but I'm confused as to why/how/when it can be created without calling that.
I'm using DDEV btw, restarted it, and rebuilt cache to try to get it to break there.
@smustgrave I agree with your assessment in #10 in all counts, thanks for chiming in.
Comment #12
tr commentedThat error seems to come from
$this->requestbeingNULLinPhpMailwhen it tries to get$this->request->serverThe request is obtained in the
PhpMailconstructor:That tells me that perhaps this is being called in a non-request context?
Regardless, the problem is the lack of a request object. I still don't see how the patch in #2 can be doing anything at all if this is a problem with the global
\Drupal::request()not containing a request object.I guess the first thing I would do here is to change
PhpMailto implementContainerFactoryPluginInterfaceand do the injection the normal way, rather than using\Drupal::request()in the constructor (was there some reason for that in the first place?) And in fact, I believe current best practice is to inject the request stack instead. See https://mglaman.dev/blog/dependency-injection-anti-patterns-drupalStill, this needs a test case - it's not clear why it's failing for you, and the instructions to reproduce in the issue summary aren't sufficient to narrow down the cause.
Note that
\Drupal::request()was added toPhpMailpretty recently, in #3226117: Uncaught RfcComplianceException when email From name contains a comma, which may be why this problem is just showing up now. I don't see any discussion in that issue about injection, and the test case added in that issue didn't reveal any problems. That's why it would be important to have a test case here which could demonstrate the problem and prove the solution.Comment #13
joelpittet@TR I'm tracking it down (slowly) but I think mailsystem is creating an instance before it has the request instance.
I agree with that idea of implementing
ContainerFactoryPluginInterface, good idea!Comment #14
joelpittet@TR, I see the problem for me (might be the same for the OP), I'll leave this for @_renify_
The problem was that mimemail overwrote the constructor
https://git.drupalcode.org/project/mimemail/-/blob/8.x-1.0-alpha5/src/Pl...
But it has been fixed already in the dev branch
https://git.drupalcode.org/project/mimemail/-/blob/8.x-1.x/src/Plugin/Ma...
@TR since you are a maintainer maybe you'd consider cutting an alpha6?
I'll move the the dev branch in the meantime.
Comment #15
joelpittetAdding the related issue it was fixed in mimemail, thanks @TR for committing it!
Comment #16
tr commentedMimeMail now has a new release. Thanks for that suggestion.
Still not sure if @_renify_ or @linkanp were using MimeMail or if they were encountering some other problem.
I also still think it's a good idea to do the injection properly in the PhpMail constructor rather than just use the global function. The only reason MimeMail had to be fixed was because MimeMail plugin needed the injected service and had to override the parent PhpMail class which wasn't doing injection properly.
Comment #17
joelpittetThanks @TR for getting that release out! I hope that fixes for the others as well!
Comment #19
igorgoncalves commentedComment #20
igorgoncalves commentedAfter facing this same issue on my project, i fall into this old thread and i just updated the steps to reproduce.
Looks like the original problem of @_renify_ was related with the mimemail module and the fix pointed by @joelpittet and @TR resolves the error.
But in my case i wasn't using the mimemail, insted, i faced the issue using the moderation_note.
As following the same trail they left here, i checked the moderation_node module and also a very similar code gap was causing the error.
So, after adding the patch into moderation_note module (just added the related issue to this thread as well), the error is gone.
Maybe at this point is enough to say that the issue its not in the core.
But i also agree that it's a good idea to do the injection properly in the PhpMail constructor, rather than just use the global function.
Should be the case to close this one as a bug report, and open a feature request mentioning this thread?
Comment #21
smustgrave commentedDon’t see the test coverage
Comment #22
igorgoncalves commentedHi @smustgrave!
Thanks for your quick reply.
I can see what you said on #10: "Will need a test case showing this issue in core though".
But i raised the idea on my last comment #20, that maybe we'll never find the way to show this issue on core.
As both scenarios we reproduce here was related to 3rd contrib modules: mimemail and moderated_note
Also, the patch #2 attached have some cons pointed by #6.
So maybe we have nothing to test here, and instead review if this original issue its a bug or can be replaced as a feature request.
Do you agree?
Comment #23
smustgrave commentedIf the bug can't be reproduced in core then, to me, sounds like the contrib modules could be the bug.
Comment #24
igorgoncalves commentedYes, i agree with you.
Both scenarios we found here should be fixed inside cobtrib modules issues, not here.
Thats why i suggest to close this Core Bug Issue and open a Core Feature request, as at least the thread serves to pointed out some core-code improvements.
Comment #25
dcam commentedAs suggested, I'm closing this issue. There is an existing issue for converting calls to
\Drupal::request()to container injection. So I'm considering this to be a duplicate.Credit has been granted to the contributors who worked on the issue.