You are here

public function TransportFactory::getTransport in Swift Mailer 8

Same name and namespace in other branches
  1. 8.2 src/TransportFactory.php \Drupal\swiftmailer\TransportFactory::getTransport()

Instantiates a transport object of the specified type.

Parameters

string $transport_type: The type of transform to instantiate.

Return value

\Swift_Transport A new instance of the specified transport type.

Overrides TransportFactoryInterface::getTransport

File

src/TransportFactory.php, line 44

Class

TransportFactory
A service that instantiates transport subsystems.

Namespace

Drupal\swiftmailer

Code

public function getTransport($transport_type) {

  // Configure the mailer based on the configured transport type.
  switch ($transport_type) {
    case SWIFTMAILER_TRANSPORT_SMTP:

      // Get transport configuration.
      $host = $this->transportConfig
        ->get('smtp_host');
      $port = $this->transportConfig
        ->get('smtp_port');
      $encryption = $this->transportConfig
        ->get('smtp_encryption');
      $provider = $this->transportConfig
        ->get('smtp_credential_provider');
      $username = NULL;
      $password = NULL;
      if ($provider === 'swiftmailer') {
        $username = $this->transportConfig
          ->get('smtp_credentials')['swiftmailer']['username'];
        $password = $this->transportConfig
          ->get('smtp_credentials')['swiftmailer']['password'];
      }
      elseif ($provider === 'key') {

        /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
        $storage = \Drupal::entityTypeManager()
          ->getStorage('key');

        /** @var \Drupal\key\KeyInterface $username_key */
        $username_key = $storage
          ->load($this->transportConfig
          ->get('smtp_credentials')['key']['username']);
        if ($username_key) {
          $username = $username_key
            ->getKeyValue();
        }

        /** @var \Drupal\key\KeyInterface $password_key */
        $password_key = $storage
          ->load($this->transportConfig
          ->get('smtp_credentials')['key']['password']);
        if ($password_key) {
          $password = $password_key
            ->getKeyValue();
        }
      }
      elseif ($provider === 'multikey') {

        /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
        $storage = \Drupal::entityTypeManager()
          ->getStorage('key');

        /** @var \Drupal\key\KeyInterface $username_key */
        $user_password_key = $storage
          ->load($this->transportConfig
          ->get('smtp_credentials')['multikey']['user_password']);
        if ($user_password_key) {
          $values = $user_password_key
            ->getKeyValues();
          $username = $values['username'];
          $password = $values['password'];
        }
      }

      // Instantiate transport.
      $transport = \Swift_SmtpTransport::newInstance($host, $port);
      $transport
        ->setLocalDomain('[127.0.0.1]');

      // Set encryption (if any).
      if (!empty($encryption)) {
        $transport
          ->setEncryption($encryption);
      }

      // Set username (if any).
      if (!empty($username)) {
        $transport
          ->setUsername($username);
      }

      // Set password (if any).
      if (!empty($password)) {
        $transport
          ->setPassword($password);
      }
      break;
    case SWIFTMAILER_TRANSPORT_SENDMAIL:

      // Get transport configuration.
      $path = $this->transportConfig
        ->get('sendmail_path');
      $mode = $this->transportConfig
        ->get('sendmail_mode');

      // Instantiate transport.
      $transport = Swift_SendmailTransport::newInstance($path . ' -' . $mode);
      break;
    case SWIFTMAILER_TRANSPORT_NATIVE:

      // Instantiate transport.
      $transport = Swift_MailTransport::newInstance();
      break;
    case SWIFTMAILER_TRANSPORT_SPOOL:

      // Instantiate transport.
      $spooldir = $this->transportConfig
        ->get('spool_directory');
      $spool = new Swift_FileSpool($spooldir);
      $transport = Swift_SpoolTransport::newInstance($spool);
      break;
    case SWIFTMAILER_TRANSPORT_NULL:
      $transport = Swift_NullTransport::newInstance();
      break;
  }
  if (!isset($transport)) {
    throw new \LogicException('The transport method is undefined.');
  }
  return $transport;
}