Commit a3a939b0 authored by mschudders's avatar mschudders Committed by Adam Shepherd
Browse files

Issue #3256740: Add an option to disable peer verification for SMTP

parent 042f1571
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -69,6 +69,25 @@ symfony_mailer.transport_plugin.smtp:
    port:
      type: integer
      label: 'Port'
    query:
      type: mapping
      label: 'URL query parameters'
      mapping:
        verify_peer:
          type: boolean
          label: 'Verify peer'
        local_domain:
          type: string
          label: 'HELO command domain name'
        restart_threshold:
          type: integer
          label: 'Restart threshold'
        restart_threshold_sleep:
          type: integer
          label: 'Restart threshold sleep'
        ping_threshold:
          type: integer
          label: 'Ping threshold'

symfony_mailer.mailer_policy.*:
  type: config_entity
+59 −14
Original line number Diff line number Diff line
@@ -24,17 +24,14 @@ class SmtpTransport extends TransportBase {
      'pass' => '',
      'host' => '',
      'port' => '',
      'query' => [
        'verify_peer' => TRUE,
        'local_domain' => '',
        'restart_threshold' => NULL,
        'restart_threshold_sleep' => NULL,
        'ping_threshold' => NULL,
      ],
    ];

    // @todo Support extra options
    // - local_domain: The domain name to use in HELO command.
    // - restart_threshold: The maximum number of messages to send before
    //   re-starting the transport.
    // - restart_threshold_sleep The number of seconds to sleep between
    //   stopping and re-starting the transport.
    // - ping_threshold: The minimum number of seconds between two messages
    //   required to ping the server.
    // - verify_peer: Disable TLS peer verification (not recommended).
  }

  /**
@@ -45,21 +42,21 @@ class SmtpTransport extends TransportBase {
      '#type' => 'textfield',
      '#title' => $this->t('User name'),
      '#default_value' => $this->configuration['user'],
      '#description' => $this->t('User name to log in'),
      '#description' => $this->t('User name to log in.'),
    ];

    $form['pass'] = [
      '#type' => 'password',
      '#title' => $this->t('Password'),
      '#default_value' => $this->configuration['pass'],
      '#description' => $this->t('Password to log in'),
      '#description' => $this->t('Password to log in.'),
    ];

    $form['host'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Host name'),
      '#default_value' => $this->configuration['host'],
      '#description' => $this->t('SMTP host name'),
      '#description' => $this->t('SMTP host name.'),
      '#required' => TRUE,
    ];

@@ -67,11 +64,54 @@ class SmtpTransport extends TransportBase {
      '#type' => 'number',
      '#title' => $this->t('Port'),
      '#default_value' => $this->configuration['port'],
      '#description' => $this->t('SMTP port'),
      '#description' => $this->t('SMTP port.'),
      '#min' => 0,
      '#max' => 65535,
    ];

    $form['query']['verify_peer'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Perform TLS peer verification'),
      '#description' => $this->t('This is recommended for security reasons, however it can be useful to disable it while developing or when using a self-signed certificate.'),
      '#default_value' => $this->configuration['query']['verify_peer'],
    ];

    $form['advanced_options'] = [
      '#type' => 'details',
      '#title' => 'Advanced options',
    ];

    $form['advanced_options']['local_domain'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Local domain'),
      '#default_value' => $this->configuration['query']['local_domain'],
      '#description' => $this->t('The domain name to use in HELO command.'),
    ];

    $form['advanced_options']['restart_threshold'] = [
      '#type' => 'number',
      '#title' => $this->t('Restart threshold'),
      '#default_value' => $this->configuration['query']['restart_threshold'],
      '#description' => $this->t('The maximum number of messages to send before re-starting the transport.'),
      '#min' => 0,
    ];

    $form['advanced_options']['restart_threshold_sleep'] = [
      '#type' => 'number',
      '#title' => $this->t('Restart threshold sleep'),
      '#default_value' => $this->configuration['query']['restart_threshold_sleep'],
      '#description' => $this->t('The number of seconds to sleep between stopping and re-starting the transport.'),
      '#min' => 0,
    ];

    $form['advanced_options']['ping_threshold'] = [
      '#type' => 'number',
      '#title' => $this->t('Ping threshold'),
      '#default_value' => $this->configuration['query']['restart_threshold_sleep'],
      '#description' => $this->t('The minimum number of seconds between two messages required to ping the server.'),
      '#min' => 0,
    ];

    return $form;
  }

@@ -83,6 +123,11 @@ class SmtpTransport extends TransportBase {
    $this->configuration['pass'] = $form_state->getValue('pass');
    $this->configuration['host'] = $form_state->getValue('host');
    $this->configuration['port'] = $form_state->getValue('port');
    $this->configuration['query']['verify_peer'] = $form_state->getValue('verify_peer');
    $this->configuration['query']['local_domain'] = $form_state->getValue('local_domain');
    $this->configuration['query']['restart_threshold'] = $form_state->getValue('restart_threshold');
    $this->configuration['query']['restart_threshold_sleep'] = $form_state->getValue('restart_threshold_sleep');
    $this->configuration['query']['ping_threshold'] = $form_state->getValue('ping_threshold');
  }

}
+7 −4
Original line number Diff line number Diff line
@@ -16,12 +16,15 @@ abstract class TransportBase extends PluginBase implements TransportPluginInterf
   */
  public function getDsn() {
    $cfg = $this->configuration;
    $query = !empty($cfg['query']) ? array_filter($cfg['query']) : [];
    $default_cfg = $this->defaultConfiguration();

    // Remove default values from query string.
    $query = !empty($cfg['query']) ? array_diff_assoc($cfg['query'], $default_cfg['query']) : [];

    $dsn = $this->getPluginId() . '://' .
      (isset($cfg['user']) ? urlencode($cfg['user']) : '') .
      (isset($cfg['pass']) ? ':' . urlencode($cfg['pass']) : '') .
      (isset($cfg['user']) ? '@' : '') .
      (!empty($cfg['user']) ? urlencode($cfg['user']) : '') .
      (!empty($cfg['pass']) ? ':' . urlencode($cfg['pass']) : '') .
      (!empty($cfg['user']) ? '@' : '') .
      (urlencode($cfg['host'] ?? 'default')) .
      (isset($cfg['port']) ? ':' . $cfg['port'] : '') .
      ($query ? '?' . http_build_query($query) : '');
+12 −0
Original line number Diff line number Diff line
@@ -110,3 +110,15 @@ function symfony_mailer_update_10004() {
    MailerTransport::create($values)->save();
  }
}

/**
 * Update smtp transports with new query configuration.
 */
function symfony_mailer_update_10005() {
  foreach (MailerTransport::loadMultiple() as $transport) {
    if ($transport->getPlugin()->getPluginId() === 'smtp') {
      // Resave smtp transports so they get new default config entries.
      $transport->save();
    }
  }
}