vendor/shopware/core/Content/Mail/Service/MailFactory.php line 114

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\Write\Validation\ConstraintBuilder;
  5. use Shopware\Core\Framework\Feature;
  6. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  7. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  8. use Symfony\Component\Mime\Email;
  9. use Symfony\Component\Validator\ConstraintViolationList;
  10. use Symfony\Component\Validator\Validator\ValidatorInterface;
  11. class MailFactory extends AbstractMailFactory
  12. {
  13.     /**
  14.      * @var ValidatorInterface
  15.      */
  16.     private $validator;
  17.     /**
  18.      * @var FilesystemInterface
  19.      */
  20.     private $filesystem;
  21.     /**
  22.      * @internal
  23.      */
  24.     public function __construct(ValidatorInterface $validatorFilesystemInterface $filesystem)
  25.     {
  26.         $this->validator $validator;
  27.         $this->filesystem $filesystem;
  28.     }
  29.     public function create(
  30.         string $subject,
  31.         array $sender,
  32.         array $recipients,
  33.         array $contents,
  34.         array $attachments,
  35.         array $additionalData,
  36.         ?array $binAttachments null
  37.     ): Email {
  38.         $this->assertValidAddresses(array_keys($recipients));
  39.         $mail = (new Email())
  40.             ->subject($subject)
  41.             ->from(...$this->formatMailAddresses($sender))
  42.             ->to(...$this->formatMailAddresses($recipients));
  43.         foreach ($contents as $contentType => $data) {
  44.             if ($contentType === 'text/html') {
  45.                 $mail->html($data);
  46.             } else {
  47.                 $mail->text($data);
  48.             }
  49.         }
  50.         $attach Feature::isActive('v6.5.0.0') ? 'attach' 'embed';
  51.         foreach ($attachments as $url) {
  52.             $mail->$attach($this->filesystem->read($url) ?: ''basename($url), $this->filesystem->getMimetype($url) ?: null);
  53.         }
  54.         if (isset($binAttachments)) {
  55.             foreach ($binAttachments as $binAttachment) {
  56.                 $mail->$attach(
  57.                     $binAttachment['content'],
  58.                     $binAttachment['fileName'],
  59.                     $binAttachment['mimeType']
  60.                 );
  61.             }
  62.         }
  63.         foreach ($additionalData as $key => $value) {
  64.             switch ($key) {
  65.                 case 'recipientsCc':
  66.                     $mail->addCc(...$this->formatMailAddresses([$value => $value]));
  67.                     break;
  68.                 case 'recipientsBcc':
  69.                     $mail->addBcc(...$this->formatMailAddresses([$value => $value]));
  70.                     break;
  71.                 case 'replyTo':
  72.                     $mail->addReplyTo(...$this->formatMailAddresses([$value => $value]));
  73.                     break;
  74.                 case 'returnPath':
  75.                     $mail->returnPath(...$this->formatMailAddresses([$value => $value]));
  76.             }
  77.         }
  78.         return $mail;
  79.     }
  80.     public function getDecorated(): AbstractMailFactory
  81.     {
  82.         throw new DecorationPatternException(self::class);
  83.     }
  84.     /**
  85.      * @throws ConstraintViolationException
  86.      */
  87.     private function assertValidAddresses(array $addresses): void
  88.     {
  89.         $constraints = (new ConstraintBuilder())
  90.             ->isNotBlank()
  91.             ->isEmail()
  92.             ->getConstraints();
  93.         $violations = new ConstraintViolationList();
  94.         foreach ($addresses as $address) {
  95.             $violations->addAll($this->validator->validate($address$constraints));
  96.         }
  97.         if ($violations->count() > 0) {
  98.             throw new ConstraintViolationException($violations$addresses);
  99.         }
  100.     }
  101.     private function formatMailAddresses(array $addresses): array
  102.     {
  103.         $formattedAddresses = [];
  104.         foreach ($addresses as $mail => $name) {
  105.             $formattedAddresses[] = $name ' <' $mail '>';
  106.         }
  107.         return $formattedAddresses;
  108.     }
  109. }