PHP Fatal Error - Cannot use positional argument after named argument in

Created on 9 October 2024, 4 months ago

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:

  1. Attempt to send an email using the graph_mail module.
  2. 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)

๐Ÿ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

๐Ÿ‡จ๐Ÿ‡ฟCzech Republic kryber

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024