Commit dfa04fc5 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3254085 by AdamPS, DavorHorvacki, imclean: Sending mails to multiple...

Issue #3254085 by AdamPS, DavorHorvacki, imclean: Sending mails to multiple email addresses does not work via BC
parent e66e946c
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ use Drupal\Core\Mail\MailManager;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\symfony_mailer\EmailFactory;
use Drupal\symfony_mailer\MailerHelperInterface;

/**
 * Provides a Symfony Mailer replacement for MailManager.
@@ -23,6 +24,13 @@ class MailManagerReplacement extends MailManager {
   */
  protected $emailFactory;

  /**
   * The mailer helper.
   *
   * @var \Drupal\symfony_mailer\MailerHelperInterface
   */
  protected $mailerHelper;

  /**
   * Constructs the MailManagerReplacement object.
   *
@@ -43,10 +51,13 @@ class MailManagerReplacement extends MailManager {
   *   The renderer.
   * @param \Drupal\symfony_mailer\EmailFactory $email_factory;
   *   The email factory.
   * @param \Drupal\symfony_mailer\MailerHelperInterface $mailer_helper
   *   The mailer helper.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, TranslationInterface $string_translation, RendererInterface $renderer, EmailFactory $email_factory) {
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, TranslationInterface $string_translation, RendererInterface $renderer, EmailFactory $email_factory, MailerHelperInterface $mailer_helper) {
    parent::__construct($namespaces, $cache_backend, $module_handler, $config_factory, $logger_factory, $string_translation, $renderer);
    $this->emailFactory = $email_factory;
    $this->mailerHelper = $mailer_helper;
  }

  /**
@@ -64,11 +75,11 @@ class MailManagerReplacement extends MailManager {
      $email = $this->emailFactory->newModuleEmail($module, $key);
    }

    $email->setTo($context['to'])
    $email->setTo(...$this->mailerHelper->parseAddress($context['to']))
      ->setLangcode($langcode)
      ->setParams($params);
    if ($context['reply']) {
      $email->setReplyTo($reply);
      $email->setReplyTo(...$this->mailerHelper->parseAddress($context['reply']));
    }

    if ($send) {
+2 −1
Original line number Diff line number Diff line
@@ -17,7 +17,8 @@ class SymfonyMailerBcServiceProvider extends ServiceProviderBase {
  public function alter(ContainerBuilder $container) {
    $definition = $container->getDefinition('plugin.manager.mail');
    $definition->setClass('Drupal\symfony_mailer_bc\MailManagerReplacement')
      ->addArgument(new Reference('email_factory'));
      ->addArgument(new Reference('email_factory'))
      ->addArgument(new Reference('symfony_mailer.helper'));
  }

}
+10 −0
Original line number Diff line number Diff line
@@ -66,6 +66,16 @@ class MailerHelper implements MailerHelperInterface {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function parseAddress(string $encoded) {
    foreach (explode(',', $encoded) as $part) {
      $addresses[] = new Address($part);
    }
    return $addresses ?: [];
  }

  /**
   * {@inheritdoc}
   */
+22 −1
Original line number Diff line number Diff line
@@ -10,7 +10,28 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
interface MailerHelperInterface {

  /**
   * Get an address using the site mail and name.
   * Parses an address string into Address structures.
   *
   * This function should only be used for back-compatibility and migration,
   * when old code has already encoded the addresses to a string. This function
   * converts back to human-readable format, ready for the symfony mailer
   * library to encode once more during sending! New code should store
   * configuration in human-readable format with a list of addresses with
   * display names.
   *
   * @todo This function is limited. It cannot handle display names, or emails
   * with characters that require special encoding.
   *
   * @param string $encoded
   *   Encoded address string.
   *
   * @return \Symfony\Component\Mime\Address[]
   *   The parsed address structures.
   */
  public function parseAddress(string $encoded);

  /**
   * Gets an address using the site mail and name.
   *
   * @return \Symfony\Component\Mime\Address
   *   The address.