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

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.     public function __construct(\Swift_Mailer $mailer, ?EventDispatcherInterface $dispatcher null)
  27.     {
  28.         trigger_deprecation(
  29.             'sylius/mailer-bundle',
  30.             '1.8',
  31.             'The Swift Mailer integration is deprecated and will be removed in 2.0. Use the Symfony Mailer integration instead.',
  32.         );
  33.         $this->mailer $mailer;
  34.         $this->dispatcher $dispatcher;
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function send(
  40.         array $recipients,
  41.         string $senderAddress,
  42.         string $senderName,
  43.         RenderedEmail $renderedEmail,
  44.         EmailInterface $email,
  45.         array $data,
  46.         array $attachments = [],
  47.         array $replyTo = [],
  48.     ): void {
  49.         $this->sendMessage(
  50.             $renderedEmail,
  51.             $senderAddress,
  52.             $senderName,
  53.             $recipients,
  54.             $replyTo,
  55.             $attachments,
  56.             $email,
  57.             $data,
  58.         );
  59.     }
  60.     public function sendWithCC(
  61.         array $recipients,
  62.         string $senderAddress,
  63.         string $senderName,
  64.         RenderedEmail $renderedEmail,
  65.         EmailInterface $email,
  66.         array $data,
  67.         array $attachments = [],
  68.         array $replyTo = [],
  69.         array $ccRecipients = [],
  70.         array $bccRecipients = [],
  71.     ): void {
  72.         $this->sendMessage(
  73.             $renderedEmail,
  74.             $senderAddress,
  75.             $senderName,
  76.             $recipients,
  77.             $replyTo,
  78.             $attachments,
  79.             $email,
  80.             $data,
  81.             $ccRecipients,
  82.             $bccRecipients,
  83.         );
  84.     }
  85.     private function sendMessage(
  86.         RenderedEmail $renderedEmail,
  87.         string $senderAddress,
  88.         string $senderName,
  89.         array $recipients,
  90.         array $replyTo,
  91.         array $attachments,
  92.         EmailInterface $email,
  93.         array $data,
  94.         array $ccRecipients = [],
  95.         array $bccRecipients = [],
  96.     ): void {
  97.         $message = (new \Swift_Message())
  98.             ->setSubject($renderedEmail->getSubject())
  99.             ->setFrom([$senderAddress => $senderName])
  100.             ->setTo($recipients)
  101.             ->setReplyTo($replyTo)
  102.         ;
  103.         if (!empty($ccRecipients)) {
  104.             $message->setCc($ccRecipients);
  105.         }
  106.         if (!empty($bccRecipients)) {
  107.             $message->setBcc($bccRecipients);
  108.         }
  109.         $message->setBody($renderedEmail->getBody(), 'text/html');
  110.         foreach ($attachments as $attachment) {
  111.             $file \Swift_Attachment::fromPath($attachment);
  112.             $message->attach($file);
  113.         }
  114.         $emailSendEvent = new EmailSendEvent($message$email$data$recipients$replyTo);
  115.         if ($this->dispatcher !== null) {
  116.             $this->dispatcher->dispatch($emailSendEventSyliusMailerEvents::EMAIL_PRE_SEND);
  117.         }
  118.         $this->mailer->send($message);
  119.         if ($this->dispatcher !== null) {
  120.             $this->dispatcher->dispatch($emailSendEventSyliusMailerEvents::EMAIL_POST_SEND);
  121.         }
  122.     }
  123. }