Commit ce95fbad authored by Ev Maslovskiy's avatar Ev Maslovskiy
Browse files

Issue #3323954 by Spleshka: Email rerouting does not work as expected

parent fceee528
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -5,8 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased changes
 - Issue #3314414: Added tests for 'reroute email' functionality.
 - #3314414: Added tests for 'reroute email' functionality.
 - #3318273: Make sure tests run on drupal.org infrastructure for the Domino module.
 - #3323954: Fixed issue with not working fallback for emails rerouting feature.

## [3.2.3]
- Fixed bug with installing Reroute Email module on hook_update().
+15 −12
Original line number Diff line number Diff line
@@ -40,12 +40,15 @@ function domino_cache_flush() {
 */
function domino_mail_alter(&$message) {
  $config = \Drupal::config('domino.settings');
  if ($config->get('application_mode') === ApplicationInterface::MODE_PRODUCTION || !$config->get('display_emails_as_messages')) {

  // Don't mess with emails sending on the production environment,
  if ($config->get('application_mode') === ApplicationInterface::MODE_PRODUCTION) {
    return;
  }

  // Show emails as messages only when they sent to test users. All other
  // users will receive emails as usual.
  if (!empty($config->get('display_emails_as_messages'))) {
    $test_mail_pattern = '/' . $config->get('test_users_email_prefix') . '.+@' . $config->get('test_users_email_domain') . '/';
    if (isset($message['body'][0]) && preg_match($test_mail_pattern, $message['to']) === 1) {
      $message['send'] = FALSE;
@@ -61,22 +64,22 @@ function domino_mail_alter(&$message) {
        $message_body = Markup::create($bodyWithLinks);
      }

      // Show email as Drupal message.
      \Drupal::messenger()->addStatus($message_body);
  }

  // Check enviroment and set config for reroute_email.
  if ($config->get('application_mode') !== ApplicationInterface::MODE_PRODUCTION) {
    $settings = \Drupal::configFactory()->get('reroute_email.settings');
    $enable = $settings->get('enable');

    // If reroute email is not enabled,
    // then email sending is disabled as fallback.
    if ($enable === FALSE) {
      $error = 'Email was not sent: Reroute Email should be enabled for this environment.';
      \Drupal::logger('domino')->error($error);
      // Don't send such emails to test users.
      $message['send'] = FALSE;
    }
  }

  // If reroute email is not enabled, then for all non-production environments
  // we disable sending emails. Normally reroute email should be enabled, so
  // this should not be an issue.
  $reroute_email_settings = \Drupal::config('reroute_email.settings');
  if (!$reroute_email_settings->get('enable')) {
    \Drupal::logger('domino')->error('Email was not sent: Reroute Email should be enabled for this environment.');
    $message['send'] = FALSE;
  }
}

/**
+0 −1
Original line number Diff line number Diff line
@@ -109,7 +109,6 @@ class RerouteEmailTest extends KernelTestBase
  private function setCurrentEnvironment($env) {
    $configs = \Drupal::configFactory()->getEditable('domino.settings');
    $configs->set('application_mode', $env)->save();
    $configs->set('display_emails_as_messages', TRUE)->save();

    $this->assertSame($configs->get('application_mode'), $env);
  }