Request object should be initialised in child constructor, if parent constructor is not being called in child plugin constructor.
Otherwise system will through warning and error.
Error: Call to a member function has() on null in Drupal\Core\Mail\Plugin\Mail\PhpMail->mail() (line 118 of /var/www/html/docroot/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php).
Warning: Attempt to read property "server" on null in Drupal\Core\Mail\Plugin\Mail\PhpMail->mail() (line 118 of /var/www/html/docroot/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php)
#0 /var/www/html/docroot/core/includes/bootstrap.inc(158): _drupal_error_handler_real(2, 'Attempt to read...', '/var/www/html/d...', 118)
#1 /var/www/html/docroot/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php(118): _drupal_error_handler(2, 'Attempt to read...', '/var/www/html/d...', 118)
#2 /var/www/html/docroot/modules/contrib/mailsystem/src/Adapter.php(50): Drupal\Core\Mail\Plugin\Mail\PhpMail->mail(Array)
#3 /var/www/html/docroot/core/lib/Drupal/Core/Mail/MailManager.php(307): Drupal\mailsystem\Adapter->mail(Array)
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | mimemail-3357841-11-test-only.patch | 1.69 KB | tr |
| #11 | mimemail-3357841-11.patch | 2.65 KB | smokris |
Comments
Comment #2
tr commentedThe RequestStack parameter was added to the PhpMail constructor in Drupal 10.1.x (and ONLY in Drupal 10.1.x) by the commit in #3226117: Uncaught RfcComplianceException when email From name contains a comma.
Setting
$this->requestlike you do in this patch will not work with core versions < 10.1.x because core PhpMail does not define therequestproperty and because PHP 8 no longer allows dynamic properties.The patch needs to work with all supported versions of Drupal core.
The automated tests of Mime Mail don't fail in Drupal 10.1. What are the conditions that cause this error message? We need a new test case that fails without your patch to prevent this from breaking in the future.
Making this a "task" because it is something we need to do to keep up with the development pre-release of Drupal 10.1.
Comment #3
tr commentedComment #4
smokrisI'm able to reproduce the above warning and error by following these steps:
Error: Call to a member function has() on nullIn #3226117: Uncaught RfcComplianceException when email From name contains a comma, Drupal Core added the
PhpMail::$requestinstance variable which gets initialized byPhpMail::__construct(). Since theMimeMailclass derives fromPhpMailbutMimeMail::__construct()doesn't callparent::__construct();, thePhpMail::$requestinstance variable remains uninitialized, resulting in the above warning and error.I think that's because there isn't a test case for sending using the
MimeMailclass. I've attached a test that should fail.Comment #5
smokrisAnd here's a patch that addresses the problem by invoking the parent constructor.
Comment #8
smokrishttps://www.drupal.org/pift-ci-job/2701615 correctly failed with the
Attempt to read property "server" on nullerror.However, https://www.drupal.org/pift-ci-job/2701616 failed with
PHPUnit\Framework\Exception: sh: 1: /usr/sbin/sendmail: not found. Unfortunately, thePhpMail::mail()method doesn't seem to be testable, since it invokesPhpMail::doMail()which invokes PHP's built-inmail()function, without any way to override it with a mock for testing.Comment #9
donbuche commentedSubscribing, same problem here.
Comment #10
tr commented@Praveen Saini: Are you working on this? If not, please unassign yourself from this issue.
@smokris: The unit test added in #3226117: Uncaught RfcComplianceException when email From name contains a comma could be subclassed and modified to test MimeMail.php instead of PhpMail.php - that would also help when it comes to tracking changes in the PhpMail parent class, because a new subclassed test would in theory track changes in the parent class. Basically, all the test needs to do at this time is to ensure the MimeMail plugin can be instantiated without error - a test like this will provide early warning in the future if the parent class constructor gets changed again.
Comment #11
smokris@TR, thanks for the tip. Here's a new patch that includes a test that's a subclass of
PhpMailTest, and which usesPhpMailTest's technique of creating a mock of the class being tested but with thedoMail()method replaced.Comment #12
tr commentedOK, let's see if the test in #11 will fail when run with the current version of Mime Mail. What I would like to see is a test that will catch the problem if the parent constructor gets changed, so the test alone should fail here, and the test + patch of MimeMail.php should succeed like in #11.
Comment #13
smokrisSince the test-only CI job failed on PHP 8.1 (as expected), and the test-and-fix CI job passed, I'll tentatively set this to RTBC.
Comment #14
rishi.kulshreshtha+1 to the patch, works as expected.
Comment #16
tr commentedCommitted. Thanks!