TransportFactory.php in Swift Mailer 8.2
File
src/TransportFactory.php
View source
<?php
namespace Drupal\swiftmailer;
use Drupal\Core\Config\ConfigFactoryInterface;
use Swift_FileSpool;
use Swift_NullTransport;
use Swift_SendmailTransport;
use Swift_SmtpTransport;
use Swift_SpoolTransport;
class TransportFactory implements TransportFactoryInterface {
protected $transportConfig;
public function __construct(ConfigFactoryInterface $config_factory) {
$this->transportConfig = $config_factory
->get('swiftmailer.transport');
}
public function getDefaultTransportMethod() {
return $this->transportConfig
->get('transport');
}
public function getTransport($transport_type) {
switch ($transport_type) {
case SWIFTMAILER_TRANSPORT_SMTP:
$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') {
$storage = \Drupal::entityTypeManager()
->getStorage('key');
$username_key = $storage
->load($this->transportConfig
->get('smtp_credentials')['key']['username']);
if ($username_key) {
$username = $username_key
->getKeyValue();
}
$password_key = $storage
->load($this->transportConfig
->get('smtp_credentials')['key']['password']);
if ($password_key) {
$password = $password_key
->getKeyValue();
}
}
elseif ($provider === 'multikey') {
$storage = \Drupal::entityTypeManager()
->getStorage('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'];
}
}
$transport = new Swift_SmtpTransport($host, $port);
$transport
->setLocalDomain('[127.0.0.1]');
if (!empty($encryption)) {
$transport
->setEncryption($encryption);
}
if (!empty($username)) {
$transport
->setUsername($username);
}
if (!empty($password)) {
$transport
->setPassword($password);
}
break;
case SWIFTMAILER_TRANSPORT_SENDMAIL:
$path = $this->transportConfig
->get('sendmail_path');
$mode = $this->transportConfig
->get('sendmail_mode');
$transport = new Swift_SendmailTransport($path . ' -' . $mode);
break;
case SWIFTMAILER_TRANSPORT_SPOOL:
$spooldir = $this->transportConfig
->get('spool_directory');
$spool = new Swift_FileSpool($spooldir);
$transport = new Swift_SpoolTransport($spool);
break;
case SWIFTMAILER_TRANSPORT_NULL:
$transport = new Swift_NullTransport();
break;
}
if (!isset($transport)) {
throw new \LogicException('The transport method is undefined.');
}
return $transport;
}
}