Problem/Motivation

API page: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Mail%21Pl...

Form submission was taking too much time on our server (about 30 sec)
and after lots of debugging. it turned out that the code of mail function in TestMailCollector.php was causing this slowness
we have a very long list of emails on our website
so loading the emails list and adding an item to it and storing the new value back was time-consuming
I commented this code and the form submission became fast on our server

I think there should be an option somewhere to turn this operation on or off
with adding a note that this operation could take time if the list of emails gets very long

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

Aghiad created an issue. See original summary.

cilefen’s picture

Title: The mail function code could slow down the mail sending operation if the list of emails gets too long » TestMailCollector is slow with lots of recipients
Component: documentation » mail system
Issue tags: +Performance

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jungle’s picture

Version: 8.9.x-dev » 9.1.x-dev
Issue tags: +Bug Smash Initiative
  1. Tagging "Bug Smash Initiative"
  2. Changing the target version to the current dev branch
jungle’s picture

Issue summary: View changes

Applying IS template

TR’s picture

Status: Active » Postponed (maintainer needs more info)

Form submission was taking too much time on our server (about 30 sec)

I don't understand how this is related to TestMailCollector. The TestMailCollector is used only in tests. It is unclear to me why this is considered a bug in Drupal core, since there is no mention of performance problems in core related to this.

If there is a performance problem in your own test cases for your own module that uses TestMailCollector, then I think the problem is with your code - the state system is perfectly suited for core's needs. However, if you are trying to send thousands of large emails in a test case then this implementation is not ideal - but why would you do that? And why would you not just subclass TestMailCollector or write your own mail collector plugin if you have specialized needs for your tests? And if you're saying that this is a problem with interactive use of your site and NOT tests, then why are you using TestMailCollector?

Without a test case demonstrating that this is a problem in core Drupal tests I would mark this as "won't fix".

jungle’s picture

Status: Postponed (maintainer needs more info) » Needs review

See the content of the whole file: core/lib/Drupal/Core/Mail/Plugin/Mail/TestMailCollector.php

<?php

namespace Drupal\Core\Mail\Plugin\Mail;

use Drupal\Core\Mail\MailInterface;

/**
 * Defines a mail backend that captures sent messages in the state system.
 *
 * This class is for running tests or for development.
 *
 * @Mail(
 *   id = "test_mail_collector",
 *   label = @Translation("Mail collector"),
 *   description = @Translation("Does not send the message, but stores it in Drupal within the state system. Used for testing.")
 * )
 */
class TestMailCollector extends PhpMail implements MailInterface {

  /**
   * {@inheritdoc}
   */
  public function mail(array $message) {
    $captured_emails = \Drupal::state()->get('system.test_mail_collector', []);
    $captured_emails[] = $message;
    \Drupal::state()->set('system.test_mail_collector', $captured_emails);

    return TRUE;
  }

}

TestMailCollector is put in the wrong place from my understanding. Its name has a prefix "Test", but it's being invoked by the MailManager as a normal Mail plugin every time, so this is a BUG and unexpected to me.

The priority could be Major at least I think.

The fix could be moving it to a module like mail_test or somewhere else.

Setting back to NR.

TR’s picture

Status: Needs review » Active

Active, not NR. No patch here... But I would still say postponed or won't fix since the TestMailCollector works as designed and I don't see any justification for re-writing it.

I am very familiar with TestMailCollector - I have been using it for almost 7 years since it was first introduced by #2187495: Use plugin system for MailInterface classes. I've subclassed it when needed (e.g. to make an HTML-capable mail collector), and I've written other @Mail plugins that act as collectors but use strategies other than the state system to store data.

TestMailCollector is a @Mail plugin. But like all plugins is not used unless you specifically try to use it. Perhaps it should be in a test module rather than in the main Core tree, but it does what it is intended to do and it does exactly what it says it does - stores emails in the state system for use in testing and development. IMO if you want to use it in circumstances it was not designed for then that's a problem with how you're using it.

When the original poster mentions form submission, that tells me that the TestMailCollector is probably being used in a non-test environment. I don't know why that is being done, which is why I marked this issue as needs more information. But this seems to be a poor choice on the part of the initial poster. If the intent was to collect mails for later sending, then TestMailCollector is the wrong choice and an application-specific collector should have been written.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.