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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Mail\Service;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\ConstraintBuilder;
  4. use Shopware\Core\Framework\Feature;
  5. use Shopware\Core\Framework\Log\Package;
  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. #[Package('system-settings')]
  12. class MailFactory extends AbstractMailFactory
  13. {
  14.     /**
  15.      * @var ValidatorInterface
  16.      */
  17.     private $validator;
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(ValidatorInterface $validator)
  22.     {
  23.         $this->validator $validator;
  24.     }
  25.     /**
  26.      * @param string[] $sender
  27.      * @param string[] $recipients
  28.      * @param string[] $contents
  29.      * @param string[] $attachments
  30.      * @param mixed[] $additionalData
  31.      * @param array<int, mixed[]>|null $binAttachments
  32.      */
  33.     public function create(
  34.         string $subject,
  35.         array $sender,
  36.         array $recipients,
  37.         array $contents,
  38.         array $attachments,
  39.         array $additionalData,
  40.         ?array $binAttachments null
  41.     ): Email {
  42.         $this->assertValidAddresses(array_keys($recipients));
  43.         $mail = (new Mail())
  44.             ->subject($subject)
  45.             ->from(...$this->formatMailAddresses($sender))
  46.             ->to(...$this->formatMailAddresses($recipients))
  47.             ->setMailAttachmentsConfig($additionalData['attachmentsConfig'] ?? null);
  48.         foreach ($contents as $contentType => $data) {
  49.             if ($contentType === 'text/html') {
  50.                 $mail->html($data);
  51.             } else {
  52.                 $mail->text($data);
  53.             }
  54.         }
  55.         $attach Feature::isActive('v6.5.0.0') ? 'attach' 'embed';
  56.         foreach ($attachments as $url) {
  57.             $mail->addAttachmentUrl($url);
  58.         }
  59.         if (isset($binAttachments)) {
  60.             foreach ($binAttachments as $binAttachment) {
  61.                 $mail->$attach(
  62.                     $binAttachment['content'],
  63.                     $binAttachment['fileName'],
  64.                     $binAttachment['mimeType']
  65.                 );
  66.             }
  67.         }
  68.         foreach ($additionalData as $key => $value) {
  69.             switch ($key) {
  70.                 case 'recipientsCc':
  71.                     $mail->addCc(...$this->formatMailAddresses([$value => $value]));
  72.                     break;
  73.                 case 'recipientsBcc':
  74.                     $mail->addBcc(...$this->formatMailAddresses([$value => $value]));
  75.                     break;
  76.                 case 'replyTo':
  77.                     $mail->addReplyTo(...$this->formatMailAddresses([$value => $value]));
  78.                     break;
  79.                 case 'returnPath':
  80.                     $mail->returnPath(...$this->formatMailAddresses([$value => $value]));
  81.             }
  82.         }
  83.         return $mail;
  84.     }
  85.     public function getDecorated(): AbstractMailFactory
  86.     {
  87.         throw new DecorationPatternException(self::class);
  88.     }
  89.     /**
  90.      * @param string[] $addresses
  91.      *
  92.      * @throws ConstraintViolationException
  93.      */
  94.     private function assertValidAddresses(array $addresses): void
  95.     {
  96.         $constraints = (new ConstraintBuilder())
  97.             ->isNotBlank()
  98.             ->isEmail()
  99.             ->getConstraints();
  100.         $violations = new ConstraintViolationList();
  101.         foreach ($addresses as $address) {
  102.             $violations->addAll($this->validator->validate($address$constraints));
  103.         }
  104.         if ($violations->count() > 0) {
  105.             throw new ConstraintViolationException($violations$addresses);
  106.         }
  107.     }
  108.     /**
  109.      * @param string[] $addresses
  110.      *
  111.      * @return string[]
  112.      */
  113.     private function formatMailAddresses(array $addresses): array
  114.     {
  115.         $formattedAddresses = [];
  116.         foreach ($addresses as $mail => $name) {
  117.             $formattedAddresses[] = $name ' <' $mail '>';
  118.         }
  119.         return $formattedAddresses;
  120.     }
  121. }