vendor/shopware/core/Content/Mail/Service/MailerTransportFactory.php line 28

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Mail\Service;
  3. use Shopware\Core\Framework\Feature;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Symfony\Component\Mailer\Transport;
  6. use Symfony\Component\Mailer\Transport\Dsn;
  7. use Symfony\Component\Mailer\Transport\SendmailTransport;
  8. use Symfony\Component\Mailer\Transport\TransportInterface;
  9. /**
  10.  * @deprecated tag:v6.5.0 - reason:remove-decorator - Will be removed in v6.5.0, use MailerTransportLoader instead.
  11.  */
  12. class MailerTransportFactory extends Transport
  13. {
  14.     private SystemConfigService $configService;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(iterable $factoriesSystemConfigService $configService)
  19.     {
  20.         parent::__construct($factories);
  21.         $this->configService $configService;
  22.     }
  23.     public function fromString(string $dsn): TransportInterface
  24.     {
  25.         if (trim($this->configService->getString('core.mailerSettings.emailAgent')) === '') {
  26.             return parent::fromString($dsn);
  27.         }
  28.         return $this->create();
  29.     }
  30.     public function create(?SystemConfigService $configService null): TransportInterface
  31.     {
  32.         Feature::triggerDeprecationOrThrow(
  33.             'v6.5.0.0',
  34.             Feature::deprecatedClassMessage(__CLASS__'v6.5.0.0')
  35.         );
  36.         if ($configService === null) {
  37.             $configService $this->configService;
  38.         }
  39.         $emailAgent $configService->getString('core.mailerSettings.emailAgent');
  40.         if ($emailAgent === '') {
  41.             $dsn = new Dsn(
  42.                 'sendmail',
  43.                 'default'
  44.             );
  45.             return $this->fromDsnObject($dsn);
  46.         }
  47.         switch ($emailAgent) {
  48.             case 'smtp':
  49.                 return $this->createSmtpTransport($configService);
  50.             case 'local':
  51.                 return new SendmailTransport($this->getSendMailCommandLineArgument($configService));
  52.             default:
  53.                 throw new \RuntimeException(sprintf('Invalid mail agent given "%s"'$emailAgent));
  54.         }
  55.     }
  56.     protected function createSmtpTransport(SystemConfigService $configService): TransportInterface
  57.     {
  58.         Feature::triggerDeprecationOrThrow(
  59.             'v6.5.0.0',
  60.             Feature::deprecatedClassMessage(__CLASS__'v6.5.0.0')
  61.         );
  62.         $dsn = new Dsn(
  63.             $this->getEncryption($configService) === 'ssl' 'smtps' 'smtp',
  64.             $configService->getString('core.mailerSettings.host'),
  65.             $configService->getString('core.mailerSettings.username'),
  66.             $configService->getString('core.mailerSettings.password'),
  67.             $configService->getInt('core.mailerSettings.port'),
  68.             $this->getEncryption($configService) !== null ? [] : ['verify_peer' => 0]
  69.         );
  70.         return $this->fromDsnObject($dsn);
  71.     }
  72.     private function getEncryption(SystemConfigService $configService): ?string
  73.     {
  74.         $encryption $configService->getString('core.mailerSettings.encryption');
  75.         switch ($encryption) {
  76.             case 'ssl':
  77.                 return 'ssl';
  78.             case 'tls':
  79.                 return 'tls';
  80.             default:
  81.                 return null;
  82.         }
  83.     }
  84.     private function getSendMailCommandLineArgument(SystemConfigService $configService): string
  85.     {
  86.         $command '/usr/sbin/sendmail ';
  87.         $option $configService->getString('core.mailerSettings.sendMailOptions');
  88.         if ($option === '') {
  89.             $option '-t';
  90.         }
  91.         if ($option !== '-bs' && $option !== '-t') {
  92.             throw new \RuntimeException(sprintf('Given sendmail option "%s" is invalid'$option));
  93.         }
  94.         return $command $option;
  95.     }
  96. }