vendor/sylius/mailer-bundle/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\MailerBundle\Sender\Adapter;
  12. use Sylius\Component\Mailer\Event\EmailSendEvent;
  13. use Sylius\Component\Mailer\Model\EmailInterface;
  14. use Sylius\Component\Mailer\Renderer\RenderedEmail;
  15. use Sylius\Component\Mailer\Sender\Adapter\AbstractAdapter;
  16. use Sylius\Component\Mailer\Sender\Adapter\CcAwareAdapterInterface;
  17. use Sylius\Component\Mailer\SyliusMailerEvents;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. /**
  20.  * @deprecated The Swift Mailer integration is deprecated since sylius/mailer-bundle 1.8. Use the Symfony Mailer integration instead.
  21.  */
  22. class SwiftMailerAdapter extends AbstractAdapter implements CcAwareAdapterInterface
  23. {
  24.     /** @var \Swift_Mailer */
  25.     protected $mailer;
  26.     /** @var EventDispatcherInterface|null */
  27.     protected $dispatcher;
  28.     public function __construct(\Swift_Mailer $mailer, ?EventDispatcherInterface $dispatcher null)
  29.     {
  30.         trigger_deprecation(
  31.             'sylius/mailer-bundle',
  32.             '1.8',
  33.             'The Swift Mailer integration is deprecated and will be removed in 2.0. Use the Symfony Mailer integration instead.',
  34.         );
  35.         $this->mailer $mailer;
  36.         $this->dispatcher $dispatcher;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function send(
  42.         array $recipients,
  43.         string $senderAddress,
  44.         string $senderName,
  45.         RenderedEmail $renderedEmail,
  46.         EmailInterface $email,
  47.         array $data,
  48.         array $attachments = [],
  49.         array $replyTo = [],
  50.     ): void {
  51.         $this->sendMessage(
  52.             $renderedEmail,
  53.             $senderAddress,
  54.             $senderName,
  55.             $recipients,
  56.             $replyTo,
  57.             $attachments,
  58.             $email,
  59.             $data,
  60.         );
  61.     }
  62.     public function sendWithCC(
  63.         array $recipients,
  64.         string $senderAddress,
  65.         string $senderName,
  66.         RenderedEmail $renderedEmail,
  67.         EmailInterface $email,
  68.         array $data,
  69.         array $attachments = [],
  70.         array $replyTo = [],
  71.         array $ccRecipients = [],
  72.         array $bccRecipients = [],
  73.     ): void {
  74.         $this->sendMessage(
  75.             $renderedEmail,
  76.             $senderAddress,
  77.             $senderName,
  78.             $recipients,
  79.             $replyTo,
  80.             $attachments,
  81.             $email,
  82.             $data,
  83.             $ccRecipients,
  84.             $bccRecipients,
  85.         );
  86.     }
  87.     private function sendMessage(
  88.         RenderedEmail $renderedEmail,
  89.         string $senderAddress,
  90.         string $senderName,
  91.         array $recipients,
  92.         array $replyTo,
  93.         array $attachments,
  94.         EmailInterface $email,
  95.         array $data,
  96.         array $ccRecipients = [],
  97.         array $bccRecipients = [],
  98.     ): void {
  99.         $message = (new \Swift_Message())
  100.             ->setSubject($renderedEmail->getSubject())
  101.             ->setFrom([$senderAddress => $senderName])
  102.             ->setTo($recipients)
  103.             ->setReplyTo($replyTo)
  104.         ;
  105.         if (!empty($ccRecipients)) {
  106.             $message->setCc($ccRecipients);
  107.         }
  108.         if (!empty($bccRecipients)) {
  109.             $message->setBcc($bccRecipients);
  110.         }
  111.         $message->setBody($renderedEmail->getBody(), 'text/html');
  112.         foreach ($attachments as $attachment) {
  113.             $file \Swift_Attachment::fromPath($attachment);
  114.             $message->attach($file);
  115.         }
  116.         $emailSendEvent = new EmailSendEvent($message$email$data$recipients$replyTo);
  117.         if ($this->dispatcher !== null) {
  118.             $this->dispatcher->dispatch($emailSendEventSyliusMailerEvents::EMAIL_PRE_SEND);
  119.         }
  120.         $this->mailer->send($message);
  121.         if ($this->dispatcher !== null) {
  122.             $this->dispatcher->dispatch($emailSendEventSyliusMailerEvents::EMAIL_POST_SEND);
  123.         }
  124.     }
  125. }