TypeError: Return value must be of type array, bool returned

Created on 9 August 2024, 4 months ago

Problem/Motivation

I receive a PHP Error with the latest release 2.0.2: TypeError: _user_registrationpassword_mail_notify(): Return value must be of type array, bool returned in _user_registrationpassword_mail_notify()

Steps to reproduce

Register as a new user which sends an email.

If I revert the version to 2.0.1 the problem does not exits. If I look at the changes in de last version (2.0.2) the function _user_registrationpassword_mail_notify() now expects an array. When I look at the Kernel Test testUserRegistrationMailsSent() its being tested for a boolean.

๐Ÿ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

๐Ÿ‡ณ๐Ÿ‡ฑNetherlands coretex Deurne

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

Merge Requests

Comments & Activities

  • Issue created by @coretex
  • ๐Ÿ‡ช๐Ÿ‡ธSpain juandels3 Seville

    The same error happened to me. When downgrading to version 2.0.1 the error is not reproduced.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany dercheffe Sersheim, Germany

    Same Issue here. IMO this is a major or even critical bug because it breaks user experience massively

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States MegaKeegMan

    Glad to find this issue, as I was also about to report it. It doesn't look like there is any reason this function needs to return an array, since the results are not actually used for anything besides to check if the array returned is empty. Additionally, I can see where some of the confusion is coming from. The mail function, run on line 622 of the .module, is documented to return a bool. However, the implementation that I am seeing from my current sites mailer is called in the MailManagerReplacement class of the symfony_mailer contrib module. So there seems to be a lot of confusion even deeper around whether or not we should be dealing with an array or a bool. I am not sure yet where exactly this confusion stems from, whether this a core change that contrib modules haven't caught up with or whether there are just a few implementations doing it wrong. My best guess so far is that everything should be moving in the direction of the mail function returning a bool, and then any of the code that does anything with the result of the mail function should be updated accordingly. Of course ensuring that our mailers are implementing the mail function correctly. There is also the possibility that the mail function is just documented incorrectly. If anyone has any background knowledge on this, that would be great.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States MegaKeegMan

    For what it's worth, the MailInterface class in Drupal core hasn't been touched for 3 years, and indicates that the mail function should return a bool, see https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co...

  • I have a quick because I don't enaough time to thing it through :

    $mail = \Drupal::service('plugin.manager.mail')->mail('user_registrationpassword', $op, $account->getEmail(), $langcode, $params, $site_mail);
    //$output = $mail['result'] ?? [];
    $output = $mail['result'] ? $mail : [];

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States MegaKeegMan

    Unfortunately, that won't work in the cases where a $mail contains a boolean, which I think it should. But I'd really like to hear a maintainers thoughts on how they want to handle this, and if there is any history behind this.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States MegaKeegMan

    I created a simple patch, which should work for anyone using a mailer that incorrectly implements the mail function, like the symfony_mailer contrib module. If Your are using another mailer that correctly implements the mail function, and the mail function returns a boolean as per the Drupal core mail interface, then this patch is not for you.

    With that being said, it should be pretty easy to re-patch for that, and I think that patch should also exist.

    Unfortunately I don't have a list of which mailer modules do what.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom JeremySkinner

    We've also run into this issue, which is causing our CI to fail. We've rolled back to 2.0.1 for now. Any chance of getting a new release with a fix for this as it breaks on all versions of Drupal 10.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States MegaKeegMan
  • First commit to issue fork.
  • Assigned to Grimreaper
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance Grimreaper France ๐Ÿ‡ซ๐Ÿ‡ท
  • Pipeline finished with Canceled
    4 months ago
    Total: 72s
    #262045
  • Pipeline finished with Failed
    4 months ago
    Total: 170s
    #262046
  • Issue was unassigned.
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance Grimreaper France ๐Ÿ‡ซ๐Ÿ‡ท

    Here is the patch from MR for Composer usage.

    I got to the same conclusions of @megakeegman.

    MR is a slightly more advanced to handle cases.

    Thanks for the review.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada noah

    We're using Mailgun to send email, and we're seeing the problem both with and without the patch because the Mailgun module returns Mailgun\Model\Message\SendResponse rather than an array or a boolean. I looked for documentation to see if there's a โ€œrightโ€ type for $return['result'], but couldn't find anything. I did find that the email_contact module dealt with the same issue just by checking if $return['result'] is empty rather than expecting a specific type:

    https://www.drupal.org/project/email_contact/issues/3468210 ๐Ÿ› Form displays sending error when using Mailgun module even though it sends correctly. Fixed

    Would that be a reasonable option here?

  • ๐Ÿ‡น๐Ÿ‡ทTurkey Umit Turkey

    +1 The same problem was observed on our sites

  • Pipeline finished with Failed
    4 months ago
    Total: 128s
    #272862
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada noah

    I've updated the MR to check if $mail['return'] is empty, rather than expecting a specific type, which should make it more flexible and compatible with, e.g. the Mailgun module. Updated patch attached.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr

    Same error making the site a bit unusable for us (at least is local and we did not deploy it yet).

    MR works fine.
    Best

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr
  • ๐Ÿ‡ฆ๐Ÿ‡ชUnited Arab Emirates ThirstySix

    #17 works fine with D10.3.x

  • ๐Ÿ‡ฒ๐Ÿ‡พMalaysia muaz91 MY

    +1 for RTBC patch #17

  • Status changed to Needs work about 1 month ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia ankitv18

    Left a comment ~~ moving into NW

  • First commit to issue fork.
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia VladimirAus Brisbane, Australia

    Based on mail() function documentation, it only can return an array.
    Please review.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States MegaKeegMan

    I did not recognize that there was a difference between MailManagerInterface and MailInterface: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Mail%21Ma...

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands coretex Deurne

    The issue is fixed with the last MR changes from VladimirAus.

  • ๐Ÿ‡ท๐Ÿ‡บRussia Andrew Answer Novosibirsk

    Hi,

    I debugged the problem and find out that Drupal return 1 (just 1) from mail() function, so I replace null coalescing operator with its equivalent isset() according to PHP documentation, and add array [] to the first option (see patch).

    The bug is gone. D10.3.10, user_registrationpassword 2.0.2.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States ElusiveMind Nashville, TN

    I think this will work. But since the calling function on 339 doesn't care about a return value and 554 is just looking for a value, should the return of the function be a bool and the returns refactored accordingly? Happy to do it, but thought it may be a better way to go.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States ElusiveMind Nashville, TN

    Patch #17 worked for me on v2.0.2 and drupal 11.0.9

    It is also, in my opinion, the correct way to fix this. Verified working.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Ron Collins

    The patch in #17 is working for m as well.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Ron Collins
Production build 0.71.5 2024