Problem/Motivation

When a comment reply is posted and one of the thread's "Replies to my comment" subscribers (COMMENT_NOTIFY_COMMENT) is a registered user whose account has no email address, _comment_notify_mailalert() passes a NULL recipient to the mail manager. On Drupal 11, Drupal\Core\Mail\Plugin\Mail\PhpMail::doMail() type-hints $to as string, so this throws and aborts the whole comment save — the commenter gets a 500 and their comment is lost:

TypeError: Drupal\Core\Mail\Plugin\Mail\PhpMail::doMail(): Argument #1 ($to)
must be of type string, null given, called in
core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php on line 123
(line 168 of core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php).

Anonymous subscribers are already forced to supply an email by comment_notify_comment_validate(), so the gap is specifically registered accounts with an empty mail (common on sites migrated from older Drupal, or programmatically-created users).

In _comment_notify_mailalert() the watcher loop resolves the recipient as:

$mail = !empty($recipient_user->getEmail()) ? $recipient_user->getEmail() : $alert->getAuthorEmail();

For a registered subscriber with no account email (and no stored anonymous author email) $mail is NULL, and there is no guard before \Drupal::service('plugin.manager.mail')->mail('comment_notify', 'comment_notify_mail', $mail, ...), so NULL reaches PhpMail::doMail(). The pre-D11 mail system tolerated a NULL $to; D11's stricter type hint does not. (strtolower($mail) a few lines up also emits a PHP 8.1+ deprecation on NULL.) The entity-author branch is already guarded by !empty($author_email); only the watcher branch is affected.

Steps to reproduce

  1. Enable Comment Notify; enable it for a content type; include "Replies to my comment" in Available alerts.
  2. As user A — a registered user whose account email is empty — post a comment on a node and choose "Replies to my comment".
  3. As user B, post a reply in that thread.
  4. On save → TypeError, comment is not saved.

Proposed resolution

Skip subscribers whose resolved email is empty, before attempting to send:

$mail = !empty($recipient_user->getEmail()) ? $recipient_user->getEmail() : $alert->getAuthorEmail();
// A registered subscriber may have no email address on their account, and the
// mail manager requires a string recipient (D11 PhpMail::doMail() type-hints
// $to as string). Skip such subscribers instead of passing NULL.
if (empty($mail)) {
  continue;
}
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jhan1112 created an issue. See original summary.

jhan1112’s picture

A simple patch...

gnuget’s picture

Status: Active » Needs review

  • gnuget committed c78452fd on 8.x-1.x
    fix: #3607949 Skip subscribers without an email address
    
    By: jhan1112
    By...
gnuget’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.