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

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