Bugs if user has blank email address

Created on 10 February 2019, almost 6 years ago
Updated 7 January 2024, 10 months ago

Motivation and problem summary

When logged in as a user with blank email address

  1. Page that has newsletter attached #2892047: User without email leads to Query condition 'simplenews_subscriber.mail IN ()' cannot be empty โ†’ => exception as below.
  2. Newsletters page on user account => same exception as below.
  3. Subscription forms - they are shown with the text box to enter an email address as if for an anonymous user. Any subscription created is not linked to the account.
  4. "Newsletters" extra field on user entity => exception as below.

[error] "PHP message: Uncaught PHP Exception Drupal\Core\Database\InvalidQueryException: "Query condition 'simplenews_subscriber.mail IN ()' cannot be empty." at /[path_to_project]/docroot/core/lib/Drupal/Core/Database/Query/Condition.php line 103"

Proposed resolution

The "obvious" solution that people started to work towards is to prevent users without email from subscribing. However a better approach is to allow subscribing but of course the subscription won't activate until an email address is set.

  1. It solves the problem of losing hidden&forced subscriptions if the user is initially created without an email.
  2. It avoids a lot of code if ($account->getEmail()) - there are places than in the initial IS

Proposed tests

  • Each of the 4 subscriber form variants
  • Subscriber without mail isn't included in subscriber counts (e.g. node tab form) and no warning/error when sending newsletter.
  • View a newsletter page
  • "Newsletters" extra field on user entity
๐Ÿ› Bug report
Status

Needs work

Version

4.0

Component

Code

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom adamps

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom adamps

    Copied from ๐Ÿ› Subscriber::loadByMail(): Argument #1 ($mail) must be of type string, null given Needs review by Berdir. Still needs tests, and probably doesn't cover all of the cases in the IS yet.

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 8
    last update 10 months ago
    2 pass, 26 fail
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance andypost

    It needs one more check to pass tests

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 8
    last update 10 months ago
    56 pass, 1 fail
  • Status changed to Needs review 10 months ago
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 8
    last update 10 months ago
    61 pass
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance andypost

    Proper fix

  • Status changed to Needs work 10 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom adamps

    Thanks. Back to needs work because several of the cases in the IS are still failing AFAICS. Also would be great to have tests.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Aditiup

    Hello Sir,
    For the issue #3031919 I propose the following solution, I guess this might solve the problem

    Changes to be made in the initial code in SubscriberForm.php

    1)Added Email Field for Anonymous Users:For anonymous users, an email field is added to the form to capture their email address.
    2)Handled Users Without Email Addresses:For logged-in users without an email address, a message is displayed indicating that their subscription will remain inactive until an email address is set.
    3)Adjusted Subscription Logic:In the submitForm method, the subscription logic now checks if the email is provided. If not, it creates the subscription with an inactive status.

    Changes to be made in the initial code in entity Subscriber.php

    1)Email Field Requirements and Unique Constraint:
    -Changes: The email field is defined as required (setRequired(TRUE)) and includes a uniqueness constraint (addConstraint('UniqueField', [])).
    -Solution: Ensures that every subscriber entity must have a valid and unique email address. This prevents the creation of entities with empty or null email values, which could cause query conditions to fail when querying by email.

    2)User and Subscription Synchronization:
    -Changes: Implemented fillFromAccount() and copyToAccount() methods to synchronize data between the subscriber entity and user accounts.
    -Solution: When a user subscribes with a blank email, these methods ensure that relevant user information (such as email address, preferred language, and status) is correctly synchronized between the user account and the subscriber entity. This synchronization prevents discrepancies that could lead to errors in subscription management.

    3)Entity Load and Creation:
    -Changes: Added loadByMail() and loadByUid() methods to facilitate loading of subscriber entities based on email or user ID.
    -Solution: These methods ensure proper entity creation and retrieval, even in cases where users have blank email addresses. They provide flexibility to create new entities if none exist, ensuring robust entity management regardless of initial user data.

    Changes to be made in the initial code in entity ConfirmationController.php

    1)Subscriber Email Check: Added checks using $subscriber->getEmail() to ensure the subscriber has a valid email address before performing subscription actions (subscribe or unsubscribe).

    2)Immediate Action Handling: Adjusted logic within the else block to include checks before calling $this->subscriptionManager->subscribe() or $this->subscriptionManager->unsubscribe() to ensure operations are only performed if the subscriber's email address exists.

    Changes to be made in query condition.php

    condition() Method Adjustment:
    1)Within the condition() method, added a check specific to simplenews_subscriber.mail field where if the value is empty (NULL or ''), the condition is skipped to prevent the `
    2)The check ensures that conditions involving simplenews_subscriber.mail are handled gracefully when the value is empty, aligning with the proposed resolution to allow subscriptions without an email but defer activation until an email is set.

  • ๐Ÿ‡น๐Ÿ‡ณTunisia Ahmed Aziz ABBASSI

    @Aditiup Could you please provide a patch file?

    Hereโ€™s an example of how to create one: git diff > filename.patch

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Aditiup
  • Pipeline finished with Failed
    5 months ago
    Total: 369s
    #214849
  • ๐Ÿ‡น๐Ÿ‡ณTunisia Ahmed Aziz ABBASSI

    @Aditiup Please do not mark your MR as a draft, as it will be automatically blocked.

  • Status changed to Needs review 5 months ago
  • ๐Ÿ‡น๐Ÿ‡ณTunisia Ahmed Aziz ABBASSI

    @Aditiup I will try to test it ASAP and set RTBC.

  • Status changed to Needs work 3 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany stefan.korn Jossgrund

    I suppose patch from #23 and MR60 are currently not really a working solution for 4.x (i don't know if they maybe worked for 3.x). But 4.x is badly broken with this patch, basically whole system is dead after applying this patch. It seems this patch attempts to do a lot of refactoring beside fixing the issues with user without mail and maybe this has not kept up with latest changes in Simple news.

    It seems to me, starting over here would be better. And for the refactoring part, imho one should focus on fixing the issues with user with no mail address here and do the refactoring in separate issue.

    When ready the issue description I am also asking myself, whether there are not some basic elements missing. It's about "when logged in as a user with blank email address". But on my end, I cannot create any user with blank email address in the first place, because this is already failing. So this would be something to solve before going to problems logging in with such a user.

    So this surely "Needs work"

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany stefan.korn Jossgrund

    Proposing this patch as a starting point, allowing to add a user without mail address and fixing #2 from issue description (by not showing the newsletters page if user has no mail address).

    My use case is to have users without mail address for other purpose than simple news, OPs case was to have users add the mail address later. So this patch is not enough for the latter case obviously.

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

    AdamPS โ†’ changed the visibility of the branch 3031919-bugs-if-user to hidden.

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

    Thanks I agree and I hid the MR/patch with lots of refactoring that is not part of this issue.

    I believe the patch from #19 is still good. It contains some extra changes to what you have in #30. It would be great to merge these together and convert to a MR (Drupal.org is stopping using patches now).

    This IS describes 4 cases so it would be good to track which ones we have done.

  • ๐Ÿ‡ฒ๐Ÿ‡ฉMoldova nick.murza Moldova

    this is my solution for this issue, I use social auth and Facebook, in some cases the email is missing, and the site crashes at the user login.

  • ๐Ÿ‡ฒ๐Ÿ‡ฉMoldova nick.murza Moldova
  • ๐Ÿ‡ฒ๐Ÿ‡ฉMoldova nick.murza Moldova
  • ๐Ÿ‡ฒ๐Ÿ‡ฉMoldova nick.murza Moldova
  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    But on my end, I cannot create any user with blank email address in the first place, because this is already failing.

    Yes, if you as an administrator create a new account, you get an error, and also if you save such a user.

    But not all users need to have a subscription.

    This should have Priority "Major":

    Trigger a PHP error through the user interface, but only under rare circumstances or affecting only a small percentage of all users, even if there is a workaround.

    From https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-... โ†’

  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Adding related discussions.

  • ๐Ÿ‡ธ๐Ÿ‡ชSweden mohammed motar

    We experienced an issue when updating a user and received the following error:
    TypeError: Drupal\simplenews\Entity\Subscriber::loadByMail():
    Argument #1 ($mail) must be of type string, null given, called in /web/modules/contrib/simplenews/simplenews.module on line 507 i Drupal\simplenews\Entity\Subscriber::loadByMail()
    (rad 460 av /web/modules/contrib/simplenews/src/Entity/Subscriber.php).

    We updated the patch and added change in simplenews_user_presave from #19.

  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Thanks @mohammed motar, perhaps you can include an interdiff between #19 patch and your patch?

Production build 0.71.5 2024