Verified Commit 749e1f03 authored by Dave Long's avatar Dave Long
Browse files

Issue #3399645 by znerol, smustgrave: Use structured DSN instead of URI in system.mail mailer_dsn

(cherry picked from commit 0b7c2a60)
parent c0bfb85b
Loading
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mime\Email;

/**
@@ -35,11 +36,16 @@
 *
 * @code
 *   $config['system.mail']['interface'] = [ 'default' => 'symfony_mailer' ];
 *   $config['system.mail']['mailer_dsn'] = 'smtp://user:pass@smtp.example.com:25';
 *   $config['system.mail']['mailer_dsn'] = [
 *     'scheme' => 'smtp',
 *     'host' => 'smtp.example.com',
 *     'port' => 25,
 *     'user' => 'user',
 *     'password' => 'pass',
 *     'options' => [],
 *   ];
 * @endcode
 *
 * Note that special characters in the mailer_dsn need to be URL encoded.
 *
 * @see https://symfony.com/doc/current/mailer.html#using-built-in-transports
 *
 * @Mail(
@@ -143,6 +149,7 @@ public function mail(array $message) {
  protected function getMailer(): MailerInterface {
    if (!isset($this->mailer)) {
      $dsn = \Drupal::config('system.mail')->get('mailer_dsn');
      $dsnObject = new Dsn(...$dsn);

      // Symfony Mailer and Transport classes both optionally depend on the
      // event dispatcher. When provided, a MessageEvent is fired whenever an
@@ -154,7 +161,9 @@ protected function getMailer(): MailerInterface {
      // mails into the code path (i.e., event subscribers) of the new API.
      // Therefore, this plugin deliberately refrains from injecting the event
      // dispatcher.
      $transport = Transport::fromDsn($dsn, logger: $this->logger);
      $factories = Transport::getDefaultFactories(logger: $this->logger);
      $transportFactory = new Transport($factories);
      $transport = $transportFactory->fromDsnObject($dsnObject);
      $this->mailer = new Mailer($transport);
    }

+8 −1
Original line number Diff line number Diff line
@@ -330,7 +330,14 @@ protected function initConfig(ContainerInterface $container) {
    // some tests expect to be able to test mail system implementations.
    $config->getEditable('system.mail')
      ->set('interface.default', 'test_mail_collector')
      ->set('mailer_dsn', 'null://null')
      ->set('mailer_dsn', [
        'scheme' => 'null',
        'host' => 'null',
        'user' => NULL,
        'password' => NULL,
        'port' => NULL,
        'options' => [],
      ])
      ->save();

    // By default, verbosely display all errors and disable all production
+6 −2
Original line number Diff line number Diff line
@@ -65,8 +65,12 @@ public function useTestMailCollector() {
      'value' => 'test_mail_collector',
      'required' => TRUE,
    ];
    $settings['config']['system.mail']['mailer_dsn'] = (object) [
      'value' => 'null://null',
    $settings['config']['system.mail']['mailer_dsn']['scheme'] = (object) [
      'value' => 'null',
      'required' => TRUE,
    ];
    $settings['config']['system.mail']['mailer_dsn']['host'] = (object) [
      'value' => 'null',
      'required' => TRUE,
    ];
    $this->writeSettings($settings);
+7 −1
Original line number Diff line number Diff line
interface:
  default: 'php_mail'
mailer_dsn: "sendmail://default"
mailer_dsn:
  scheme: 'sendmail'
  host: 'default'
  user: null
  password: null
  port: null
  options: []
+38 −1
Original line number Diff line number Diff line
@@ -304,8 +304,45 @@ system.mail:
        type: string
        label: 'Interface'
    mailer_dsn:
      type: string
      type: mapping
      label: 'Symfony mailer transport DSN'
      mapping:
        scheme:
          type: string
          label: 'Scheme'
          constraints:
            NotBlank:
              message: 'The mailer DSN must contain a scheme.'
        host:
          type: string
          label: 'Host'
          constraints:
            NotBlank:
              message: 'The mailer DSN must contain a host (use "default" by default).'
        user:
          type: string
          nullable: true
          label: 'User'
        password:
          type: string
          nullable: true
          label: 'Password'
        port:
          type: integer
          nullable: true
          label: 'Port'
          constraints:
            Range:
              min: 0
              max: 65535
        options:
          type: sequence
          label: 'Options'
          sequence:
            type: string
            label: Option
            constraints:
              NotNull: []

system.theme.global:
  type: theme_settings
Loading