Description:
While attempting to send emails using the graph_mail
module via the Microsoft Graph API, a fatal PHP error is triggered due to a mix of positional and named arguments in a method call. Specifically, the error occurs in the MailHelper.php
file at line 299:
PHP Fatal error: Cannot use positional argument after named argument in C:\inetpub\wwwroot\rtdev.euaa.europa.eu\web\modules\contrib\graph_mail\src\MailHelper.php on line 299
The error stems from the usage of a named argument (requestType
) followed by a positional argument. In PHP 8.0 and later, mixing positional and named arguments in this way is not allowed.
Steps to Reproduce:
- Attempt to send an email using the
graph_mail
module.
- PHP will throw a fatal error preventing the mail from being sent.
Affected Code:
The issue occurs in the following line within the send()
method in MailHelper.php
:
<?php
$graph->createRequest(requestType: 'POST', '/users/' . $config->get('user_id') . '/sendmail')
->attachBody($mail_body)
->execute();
?>
Suggested Solution:
To resolve the issue, change the named argument (requestType: 'POST'
) to a positional argument. Hereโs the corrected line of code:
<?php
$graph->createRequest('POST', '/users/' . $config->get('user_id') . '/sendmail')
->attachBody($mail_body)
->execute();
?>
Detailed Explanation:
In PHP 8.0, named arguments were introduced, allowing you to pass arguments to a function using the parameter name. However, if you use named arguments, all subsequent arguments must also be named. Mixing positional arguments after named arguments is not allowed, and this is what causes the error.
In the original code, requestType: 'POST'
is a named argument, followed by the positional argument '/users/' . $config->get('user_id') . '/sendmail'
, which triggers the fatal error.
By changing requestType: 'POST'
to a positional argument ('POST'
), the function call adheres to the positional argument rules, fixing the error.
Full Code Example (Corrected):
<?php
$graph->createRequest('POST', '/users/' . $config->get('user_id') . '/sendmail')
->attachBody($mail_body)
->execute();
?>
This change resolves the issue and ensures compatibility with PHP 8.0 and later (tested on PHP 8.3)