From c84a5e5b6b44f495a97ca0c95838a4e1e8534932 Mon Sep 17 00:00:00 2001 From: Marcin Warzybok Date: Sun, 8 Dec 2019 13:54:36 +0100 Subject: [PATCH 1/4] Add handling CC and BCC to Sender --- .../Sender/Adapter/SwiftMailerAdapter.php | 7 +++++-- .../Sender/Adapter/AdapterInterface.php | 2 ++ src/Component/Sender/Sender.php | 13 +++++++++++-- src/Component/Sender/SenderInterface.php | 12 +++++++++++- src/Component/spec/Sender/SenderSpec.php | 17 ++++++++++++++--- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php b/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php index 8dbc727..c1d5563 100644 --- a/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php +++ b/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php @@ -55,13 +55,16 @@ public function send( array $data, array $attachments = [], array $replyTo = [], + array $ccRecipients = [], + array $bccRecipients = [] ): void { $message = (new \Swift_Message()) ->setSubject($renderedEmail->getSubject()) ->setFrom([$senderAddress => $senderName]) ->setTo($recipients) - ->setReplyTo($replyTo) - ; + ->setCc($ccRecipients) + ->setBcc($bccRecipients) + ->setReplyTo($replyTo); $message->setBody($renderedEmail->getBody(), 'text/html'); diff --git a/src/Component/Sender/Adapter/AdapterInterface.php b/src/Component/Sender/Adapter/AdapterInterface.php index 4795f03..da0cc72 100644 --- a/src/Component/Sender/Adapter/AdapterInterface.php +++ b/src/Component/Sender/Adapter/AdapterInterface.php @@ -32,5 +32,7 @@ public function send( array $data, array $attachments = [], array $replyTo = [], + array $ccRecipients = [], + array $bccRecipients = [] ): void; } diff --git a/src/Component/Sender/Sender.php b/src/Component/Sender/Sender.php index adb66b5..75b4d7b 100644 --- a/src/Component/Sender/Sender.php +++ b/src/Component/Sender/Sender.php @@ -44,8 +44,15 @@ public function __construct( /** * {@inheritdoc} */ - public function send(string $code, array $recipients, array $data = [], array $attachments = [], array $replyTo = []): void - { + public function send( + string $code, + array $recipients, + array $data = [], + array $attachments = [], + array $replyTo = [], + array $ccRecipients = [], + array $bccRecipients = [] + ): void { Assert::allStringNotEmpty($recipients); $email = $this->provider->getEmail($code); @@ -68,6 +75,8 @@ public function send(string $code, array $recipients, array $data = [], array $a $data, $attachments, $replyTo, + $ccRecipients, + $bccRecipients ); } } diff --git a/src/Component/Sender/SenderInterface.php b/src/Component/Sender/SenderInterface.php index c118007..e81f299 100644 --- a/src/Component/Sender/SenderInterface.php +++ b/src/Component/Sender/SenderInterface.php @@ -19,6 +19,16 @@ interface SenderInterface * @param string[]|null[] $recipients A list of email addresses to receive the message. Providing null or empty string in the list of recipients is deprecated and will be removed in SyliusMailerBundle 2.0 * @param string[] $attachments A list of file paths to attach to the message. * @param string[] $replyTo A list of email addresses to set as the Reply-To address for the message. + * @param string[] $ccRecipients A list of email addressed as carbon copy + * @param string[] $bccRecipients A list of email addressed as blind carbon copy */ - public function send(string $code, array $recipients, array $data = [], array $attachments = [], array $replyTo = []): void; + public function send( + string $code, + array $recipients, + array $data = [], + array $attachments = [], + array $replyTo = [], + array $ccRecipients = [], + array $bccRecipients = [] + ): void; } diff --git a/src/Component/spec/Sender/SenderSpec.php b/src/Component/spec/Sender/SenderSpec.php index d897057..9d79ed6 100644 --- a/src/Component/spec/Sender/SenderSpec.php +++ b/src/Component/spec/Sender/SenderSpec.php @@ -48,9 +48,20 @@ function it_sends_an_email_through_the_adapter( $data = ['foo' => 2]; $rendererAdapter->render($email, ['foo' => 2])->willReturn($renderedEmail); - $senderAdapter->send(['john@example.com'], 'sender@example.com', 'Sender', $renderedEmail, $email, $data, [], [])->shouldBeCalled(); - - $this->send('bar', ['john@example.com'], $data, []); + $senderAdapter->send( + ['john@example.com'], + 'sender@example.com', + 'Sender', + $renderedEmail, + $email, + $data, + [], + [], + ['cc@mail.com'], + ['bcc@mail.com'] + )->shouldBeCalled(); + + $this->send('bar', ['john@example.com'], $data, [], [], ['cc@mail.com'], ['bcc@mail.com']); } function it_does_not_send_disabled_emails( From 16e2e41efc240b79af8a35cb77f12446b34dff51 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Tue, 21 Jun 2022 10:31:35 +0200 Subject: [PATCH 2/4] Move CC and BCC sending to new interface and method --- src/Bundle/Sender/Adapter/DefaultAdapter.php | 18 ++++++ .../Sender/Adapter/SwiftMailerAdapter.php | 47 ++++++++++++++-- .../Sender/Adapter/SymfonyMailerAdapter.php | 44 ++++++++++++++- .../Sender/Adapter/DefaultAdapterSpec.php | 8 +++ .../Sender/Adapter/SwiftMailerAdapterSpec.php | 55 ++++++++++++++++++- .../Adapter/SymfonyMailerAdapterSpec.php | 43 ++++++++++++++- .../Sender/Adapter/AdapterInterface.php | 4 +- .../Adapter/CcAwareAdapterInterface.php | 31 +++++++++++ 8 files changed, 240 insertions(+), 10 deletions(-) create mode 100644 src/Component/Sender/Adapter/CcAwareAdapterInterface.php diff --git a/src/Bundle/Sender/Adapter/DefaultAdapter.php b/src/Bundle/Sender/Adapter/DefaultAdapter.php index 3e73f14..975dee5 100644 --- a/src/Bundle/Sender/Adapter/DefaultAdapter.php +++ b/src/Bundle/Sender/Adapter/DefaultAdapter.php @@ -43,4 +43,22 @@ public function send( SymfonyMailerAdapter::class, )); } + + public function sendWithCC( + array $recipients, + string $senderAddress, + string $senderName, + RenderedEmail $renderedEmail, + EmailInterface $email, + array $data, + array $attachments = [], + array $replyTo = [], + array $ccRecipients = [], + array $bccRecipients = [] + ): void { + throw new \RuntimeException(sprintf( + 'You need to configure an adapter to send the email. Take a look at %s (requires "symfony/mailer" library).', + SymfonyMailerAdapter::class + )); + } } diff --git a/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php b/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php index c1d5563..f65a573 100644 --- a/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php +++ b/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php @@ -17,13 +17,14 @@ use Sylius\Component\Mailer\Model\EmailInterface; use Sylius\Component\Mailer\Renderer\RenderedEmail; use Sylius\Component\Mailer\Sender\Adapter\AbstractAdapter; +use Sylius\Component\Mailer\Sender\Adapter\CcAwareAdapterInterface; use Sylius\Component\Mailer\SyliusMailerEvents; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** * @deprecated The Swift Mailer integration is deprecated since sylius/mailer-bundle 1.8. Use the Symfony Mailer integration instead. */ -class SwiftMailerAdapter extends AbstractAdapter +class SwiftMailerAdapter extends AbstractAdapter implements CcAwareAdapterInterface { /** @var \Swift_Mailer */ protected $mailer; @@ -47,6 +48,21 @@ public function __construct(\Swift_Mailer $mailer, ?EventDispatcherInterface $di * {@inheritdoc} */ public function send( + array $recipients, + string $senderAddress, + string $senderName, + RenderedEmail $renderedEmail, + EmailInterface $email, + array $data, + array $attachments = [], + array $replyTo = [] + ): void { + $this->sendMessage( + $renderedEmail, $senderAddress, $senderName, $recipients, $replyTo, $attachments, $email, $data + ); + } + + public function sendWithCC( array $recipients, string $senderAddress, string $senderName, @@ -57,14 +73,37 @@ public function send( array $replyTo = [], array $ccRecipients = [], array $bccRecipients = [] + ): void { + $this->sendMessage( + $renderedEmail, $senderAddress, $senderName, $recipients, $replyTo, $attachments, $email, $data, $ccRecipients, $bccRecipients + ); + } + + private function sendMessage( + RenderedEmail $renderedEmail, + string $senderAddress, + string $senderName, + array $recipients, + array $replyTo, + array $attachments, + EmailInterface $email, + array $data, + array $ccRecipients = [], + array $bccRecipients = [] ): void { $message = (new \Swift_Message()) ->setSubject($renderedEmail->getSubject()) ->setFrom([$senderAddress => $senderName]) ->setTo($recipients) - ->setCc($ccRecipients) - ->setBcc($bccRecipients) - ->setReplyTo($replyTo); + ->setReplyTo($replyTo) + ; + + if (!empty($ccRecipients)) { + $message->setCc($ccRecipients); + } + if (!empty($bccRecipients)) { + $message->setBcc($bccRecipients); + } $message->setBody($renderedEmail->getBody(), 'text/html'); diff --git a/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php b/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php index a64dfde..a5482b4 100644 --- a/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php +++ b/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php @@ -17,12 +17,13 @@ use Sylius\Component\Mailer\Model\EmailInterface; use Sylius\Component\Mailer\Renderer\RenderedEmail; use Sylius\Component\Mailer\Sender\Adapter\AbstractAdapter; +use Sylius\Component\Mailer\Sender\Adapter\CcAwareAdapterInterface; use Sylius\Component\Mailer\SyliusMailerEvents; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; -final class SymfonyMailerAdapter extends AbstractAdapter +final class SymfonyMailerAdapter extends AbstractAdapter implements CcAwareAdapterInterface { private MailerInterface $mailer; @@ -43,6 +44,40 @@ public function send( array $data, array $attachments = [], array $replyTo = [], + ): void { + $this->sendMessage( + $renderedEmail, $senderAddress, $senderName, $recipients, $replyTo, $attachments, $email, $data + ); + } + + public function sendWithCC( + array $recipients, + string $senderAddress, + string $senderName, + RenderedEmail $renderedEmail, + EmailInterface $email, + array $data, + array $attachments = [], + array $replyTo = [], + array $ccRecipients = [], + array $bccRecipients = [], + ): void { + $this->sendMessage( + $renderedEmail, $senderAddress, $senderName, $recipients, $replyTo, $attachments, $email, $data, $ccRecipients, $bccRecipients + ); + } + + private function sendMessage( + RenderedEmail $renderedEmail, + string $senderAddress, + string $senderName, + array $recipients, + array $replyTo, + array $attachments, + EmailInterface $email, + array $data, + array $ccRecipients = [], + array $bccRecipients = [], ): void { $message = (new Email()) ->subject($renderedEmail->getSubject()) @@ -52,6 +87,13 @@ public function send( ->html($renderedEmail->getBody()) ; + if (!empty($ccRecipients)) { + $message->addCc(...$ccRecipients); + } + if (!empty($bccRecipients)) { + $message->addBcc(...$bccRecipients); + } + foreach ($attachments as $attachment) { $message->attachFromPath($attachment); } diff --git a/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php b/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php index 931346b..1e96b4a 100644 --- a/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php +++ b/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php @@ -37,5 +37,13 @@ function it_throws_an_exception_about_not_configured_email_sender_adapter( ))) ->during('send', [['pawel@sylius.com'], 'arnaud@sylius.com', 'arnaud', $renderedEmail, $email, []]) ; + + $this + ->shouldThrow(new \RuntimeException(sprintf( + 'You need to configure an adapter to send the email. Take a look at %s (requires "symfony/mailer" library).', + SymfonyMailerAdapter::class + ))) + ->during('sendWithCc', [['pawel@sylius.com'], 'arnaud@sylius.com', 'arnaud', $renderedEmail, $email, [], [], [], ['cc@example.com'], ['bcc@example.com']]) + ; } } diff --git a/src/Bundle/spec/Sender/Adapter/SwiftMailerAdapterSpec.php b/src/Bundle/spec/Sender/Adapter/SwiftMailerAdapterSpec.php index 7962d0b..42fb876 100644 --- a/src/Bundle/spec/Sender/Adapter/SwiftMailerAdapterSpec.php +++ b/src/Bundle/spec/Sender/Adapter/SwiftMailerAdapterSpec.php @@ -50,7 +50,14 @@ function it_sends_an_email( ->shouldBeCalled() ; - $mailer->send(Argument::type('\Swift_Message'))->shouldBeCalled(); + $mailer->send(Argument::that(function(\Swift_Message $message): bool { + return + $message->getSubject() === 'subject' && + $message->getBody() === 'body' && + $message->getFrom() === ['arnaud@sylius.com' => 'arnaud'] && + $message->getTo() === ['pawel@sylius.com' => null] + ; + }))->shouldBeCalled(); $dispatcher ->dispatch(Argument::type(EmailSendEvent::class), SyliusMailerEvents::EMAIL_POST_SEND) @@ -66,4 +73,50 @@ function it_sends_an_email( [], ); } + + function it_sends_an_email_with_cc_and_bcc_emails( + \Swift_Mailer $mailer, + EmailInterface $email, + EventDispatcherInterface $dispatcher, + RenderedEmail $renderedEmail + ): void { + $this->setEventDispatcher($dispatcher); + + $renderedEmail->getSubject()->shouldBeCalled()->willReturn('subject'); + $renderedEmail->getBody()->shouldBeCalled()->willReturn('body'); + + $dispatcher + ->dispatch(Argument::type(EmailSendEvent::class), SyliusMailerEvents::EMAIL_PRE_SEND) + ->shouldBeCalled() + ; + + $mailer->send(Argument::that(function(\Swift_Message $message): bool { + return + $message->getSubject() === 'subject' && + $message->getBody() === 'body' && + $message->getFrom() === ['arnaud@sylius.com' => 'arnaud'] && + $message->getTo() === ['pawel@sylius.com' => null] && + $message->getCc() === ['cc@example.com' => null] && + $message->getBcc() === ['bcc@example.com' => null] + ; + }))->shouldBeCalled(); + + $dispatcher + ->dispatch(Argument::type(EmailSendEvent::class), SyliusMailerEvents::EMAIL_POST_SEND) + ->shouldBeCalled() + ; + + $this->sendWithCC( + ['pawel@sylius.com'], + 'arnaud@sylius.com', + 'arnaud', + $renderedEmail, + $email, + [], + [], + [], + ['cc@example.com'], + ['bcc@example.com'] + ); + } } diff --git a/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php b/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php index 61c882f..a112511 100644 --- a/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php +++ b/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php @@ -22,6 +22,7 @@ use Sylius\Component\Mailer\SyliusMailerEvents; use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; @@ -53,7 +54,14 @@ function it_sends_an_email_with_events( ->shouldBeCalled() ; - $mailer->send(Argument::type(Email::class))->shouldBeCalled(); + $mailer->send(Argument::that(function(Email $message): bool { + return + $message->getSubject() === 'subject' && + $message->getBody()->bodyToString() === 'body' && + $message->getFrom()[0] == new Address('arnaud@sylius.com', 'arnaud') && + $message->getTo()[0] == new Address('pawel@sylius.com') + ; + }))->shouldBeCalled(); $dispatcher ->dispatch(Argument::type(EmailSendEvent::class), SyliusMailerEvents::EMAIL_POST_SEND) @@ -70,6 +78,39 @@ function it_sends_an_email_with_events( ); } + function it_sends_an_email_with_cc_and_bcc( + MailerInterface $mailer, + EmailInterface $email, + RenderedEmail $renderedEmail + ): void { + $renderedEmail->getSubject()->willReturn('subject'); + $renderedEmail->getBody()->willReturn('body'); + + $mailer->send(Argument::that(function(Email $message): bool { + return + $message->getSubject() === 'subject' && + $message->getBody()->bodyToString() === 'body' && + $message->getFrom()[0] == new Address('arnaud@sylius.com', 'arnaud') && + $message->getTo()[0] == new Address('pawel@sylius.com') && + $message->getCc()[0] == new Address('cc@example.com') && + $message->getBcc()[0] == new Address('bcc@example.com') + ; + }))->shouldBeCalled(); + + $this->sendWithCC( + ['pawel@sylius.com'], + 'arnaud@sylius.com', + 'arnaud', + $renderedEmail, + $email, + [], + [], + [], + ['cc@example.com'], + ['bcc@example.com'] + ); + } + function it_sends_an_email_with_attachments( MailerInterface $mailer, EmailInterface $email, diff --git a/src/Component/Sender/Adapter/AdapterInterface.php b/src/Component/Sender/Adapter/AdapterInterface.php index da0cc72..f855090 100644 --- a/src/Component/Sender/Adapter/AdapterInterface.php +++ b/src/Component/Sender/Adapter/AdapterInterface.php @@ -31,8 +31,6 @@ public function send( EmailInterface $email, array $data, array $attachments = [], - array $replyTo = [], - array $ccRecipients = [], - array $bccRecipients = [] + array $replyTo = [] ): void; } diff --git a/src/Component/Sender/Adapter/CcAwareAdapterInterface.php b/src/Component/Sender/Adapter/CcAwareAdapterInterface.php new file mode 100644 index 0000000..8d36172 --- /dev/null +++ b/src/Component/Sender/Adapter/CcAwareAdapterInterface.php @@ -0,0 +1,31 @@ + Date: Tue, 21 Jun 2022 14:27:30 +0200 Subject: [PATCH 3/4] Remove BC Break from SenderInterface --- src/Bundle/Sender/Adapter/DefaultAdapter.php | 4 +- .../Sender/Adapter/SwiftMailerAdapter.php | 26 ++++++++++--- .../Sender/Adapter/SymfonyMailerAdapter.php | 20 +++++++++- .../Sender/Adapter/DefaultAdapterSpec.php | 2 +- .../Sender/Adapter/SwiftMailerAdapterSpec.php | 8 ++-- .../Adapter/SymfonyMailerAdapterSpec.php | 8 ++-- .../Sender/Adapter/AdapterInterface.php | 2 +- .../Adapter/CcAwareAdapterInterface.php | 11 +++++- src/Component/Sender/Sender.php | 24 ++++++++++-- src/Component/Sender/SenderInterface.php | 4 -- src/Component/spec/Sender/SenderSpec.php | 37 +++++++++++++++++-- 11 files changed, 114 insertions(+), 32 deletions(-) diff --git a/src/Bundle/Sender/Adapter/DefaultAdapter.php b/src/Bundle/Sender/Adapter/DefaultAdapter.php index 975dee5..6337115 100644 --- a/src/Bundle/Sender/Adapter/DefaultAdapter.php +++ b/src/Bundle/Sender/Adapter/DefaultAdapter.php @@ -54,11 +54,11 @@ public function sendWithCC( array $attachments = [], array $replyTo = [], array $ccRecipients = [], - array $bccRecipients = [] + array $bccRecipients = [], ): void { throw new \RuntimeException(sprintf( 'You need to configure an adapter to send the email. Take a look at %s (requires "symfony/mailer" library).', - SymfonyMailerAdapter::class + SymfonyMailerAdapter::class, )); } } diff --git a/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php b/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php index f65a573..b1f70f5 100644 --- a/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php +++ b/src/Bundle/Sender/Adapter/SwiftMailerAdapter.php @@ -55,10 +55,17 @@ public function send( EmailInterface $email, array $data, array $attachments = [], - array $replyTo = [] + array $replyTo = [], ): void { $this->sendMessage( - $renderedEmail, $senderAddress, $senderName, $recipients, $replyTo, $attachments, $email, $data + $renderedEmail, + $senderAddress, + $senderName, + $recipients, + $replyTo, + $attachments, + $email, + $data, ); } @@ -72,10 +79,19 @@ public function sendWithCC( array $attachments = [], array $replyTo = [], array $ccRecipients = [], - array $bccRecipients = [] + array $bccRecipients = [], ): void { $this->sendMessage( - $renderedEmail, $senderAddress, $senderName, $recipients, $replyTo, $attachments, $email, $data, $ccRecipients, $bccRecipients + $renderedEmail, + $senderAddress, + $senderName, + $recipients, + $replyTo, + $attachments, + $email, + $data, + $ccRecipients, + $bccRecipients, ); } @@ -89,7 +105,7 @@ private function sendMessage( EmailInterface $email, array $data, array $ccRecipients = [], - array $bccRecipients = [] + array $bccRecipients = [], ): void { $message = (new \Swift_Message()) ->setSubject($renderedEmail->getSubject()) diff --git a/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php b/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php index a5482b4..4ce5e36 100644 --- a/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php +++ b/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php @@ -46,7 +46,14 @@ public function send( array $replyTo = [], ): void { $this->sendMessage( - $renderedEmail, $senderAddress, $senderName, $recipients, $replyTo, $attachments, $email, $data + $renderedEmail, + $senderAddress, + $senderName, + $recipients, + $replyTo, + $attachments, + $email, + $data, ); } @@ -63,7 +70,16 @@ public function sendWithCC( array $bccRecipients = [], ): void { $this->sendMessage( - $renderedEmail, $senderAddress, $senderName, $recipients, $replyTo, $attachments, $email, $data, $ccRecipients, $bccRecipients + $renderedEmail, + $senderAddress, + $senderName, + $recipients, + $replyTo, + $attachments, + $email, + $data, + $ccRecipients, + $bccRecipients, ); } diff --git a/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php b/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php index 1e96b4a..3b06fe8 100644 --- a/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php +++ b/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php @@ -41,7 +41,7 @@ function it_throws_an_exception_about_not_configured_email_sender_adapter( $this ->shouldThrow(new \RuntimeException(sprintf( 'You need to configure an adapter to send the email. Take a look at %s (requires "symfony/mailer" library).', - SymfonyMailerAdapter::class + SymfonyMailerAdapter::class, ))) ->during('sendWithCc', [['pawel@sylius.com'], 'arnaud@sylius.com', 'arnaud', $renderedEmail, $email, [], [], [], ['cc@example.com'], ['bcc@example.com']]) ; diff --git a/src/Bundle/spec/Sender/Adapter/SwiftMailerAdapterSpec.php b/src/Bundle/spec/Sender/Adapter/SwiftMailerAdapterSpec.php index 42fb876..1760171 100644 --- a/src/Bundle/spec/Sender/Adapter/SwiftMailerAdapterSpec.php +++ b/src/Bundle/spec/Sender/Adapter/SwiftMailerAdapterSpec.php @@ -50,7 +50,7 @@ function it_sends_an_email( ->shouldBeCalled() ; - $mailer->send(Argument::that(function(\Swift_Message $message): bool { + $mailer->send(Argument::that(function (\Swift_Message $message): bool { return $message->getSubject() === 'subject' && $message->getBody() === 'body' && @@ -78,7 +78,7 @@ function it_sends_an_email_with_cc_and_bcc_emails( \Swift_Mailer $mailer, EmailInterface $email, EventDispatcherInterface $dispatcher, - RenderedEmail $renderedEmail + RenderedEmail $renderedEmail, ): void { $this->setEventDispatcher($dispatcher); @@ -90,7 +90,7 @@ function it_sends_an_email_with_cc_and_bcc_emails( ->shouldBeCalled() ; - $mailer->send(Argument::that(function(\Swift_Message $message): bool { + $mailer->send(Argument::that(function (\Swift_Message $message): bool { return $message->getSubject() === 'subject' && $message->getBody() === 'body' && @@ -116,7 +116,7 @@ function it_sends_an_email_with_cc_and_bcc_emails( [], [], ['cc@example.com'], - ['bcc@example.com'] + ['bcc@example.com'], ); } } diff --git a/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php b/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php index a112511..fbe143c 100644 --- a/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php +++ b/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php @@ -54,7 +54,7 @@ function it_sends_an_email_with_events( ->shouldBeCalled() ; - $mailer->send(Argument::that(function(Email $message): bool { + $mailer->send(Argument::that(function (Email $message): bool { return $message->getSubject() === 'subject' && $message->getBody()->bodyToString() === 'body' && @@ -81,12 +81,12 @@ function it_sends_an_email_with_events( function it_sends_an_email_with_cc_and_bcc( MailerInterface $mailer, EmailInterface $email, - RenderedEmail $renderedEmail + RenderedEmail $renderedEmail, ): void { $renderedEmail->getSubject()->willReturn('subject'); $renderedEmail->getBody()->willReturn('body'); - $mailer->send(Argument::that(function(Email $message): bool { + $mailer->send(Argument::that(function (Email $message): bool { return $message->getSubject() === 'subject' && $message->getBody()->bodyToString() === 'body' && @@ -107,7 +107,7 @@ function it_sends_an_email_with_cc_and_bcc( [], [], ['cc@example.com'], - ['bcc@example.com'] + ['bcc@example.com'], ); } diff --git a/src/Component/Sender/Adapter/AdapterInterface.php b/src/Component/Sender/Adapter/AdapterInterface.php index f855090..4795f03 100644 --- a/src/Component/Sender/Adapter/AdapterInterface.php +++ b/src/Component/Sender/Adapter/AdapterInterface.php @@ -31,6 +31,6 @@ public function send( EmailInterface $email, array $data, array $attachments = [], - array $replyTo = [] + array $replyTo = [], ): void; } diff --git a/src/Component/Sender/Adapter/CcAwareAdapterInterface.php b/src/Component/Sender/Adapter/CcAwareAdapterInterface.php index 8d36172..0b9042d 100644 --- a/src/Component/Sender/Adapter/CcAwareAdapterInterface.php +++ b/src/Component/Sender/Adapter/CcAwareAdapterInterface.php @@ -1,5 +1,14 @@ provider->getEmail($code); @@ -66,6 +67,23 @@ public function send( $renderedEmail = $this->rendererAdapter->render($email, $data); + if (count($arguments) > 5 && $this->senderAdapter instanceof CcAwareAdapterInterface) { + $this->senderAdapter->sendWithCC( + $recipients, + $senderAddress, + $senderName, + $renderedEmail, + $email, + $data, + $attachments, + $replyTo, + $arguments[5] ?? [], + $arguments[6] ?? [], + ); + + return; + } + $this->senderAdapter->send( $recipients, $senderAddress, @@ -75,8 +93,6 @@ public function send( $data, $attachments, $replyTo, - $ccRecipients, - $bccRecipients ); } } diff --git a/src/Component/Sender/SenderInterface.php b/src/Component/Sender/SenderInterface.php index e81f299..184400c 100644 --- a/src/Component/Sender/SenderInterface.php +++ b/src/Component/Sender/SenderInterface.php @@ -19,8 +19,6 @@ interface SenderInterface * @param string[]|null[] $recipients A list of email addresses to receive the message. Providing null or empty string in the list of recipients is deprecated and will be removed in SyliusMailerBundle 2.0 * @param string[] $attachments A list of file paths to attach to the message. * @param string[] $replyTo A list of email addresses to set as the Reply-To address for the message. - * @param string[] $ccRecipients A list of email addressed as carbon copy - * @param string[] $bccRecipients A list of email addressed as blind carbon copy */ public function send( string $code, @@ -28,7 +26,5 @@ public function send( array $data = [], array $attachments = [], array $replyTo = [], - array $ccRecipients = [], - array $bccRecipients = [] ): void; } diff --git a/src/Component/spec/Sender/SenderSpec.php b/src/Component/spec/Sender/SenderSpec.php index 9d79ed6..8fa17a4 100644 --- a/src/Component/spec/Sender/SenderSpec.php +++ b/src/Component/spec/Sender/SenderSpec.php @@ -20,7 +20,7 @@ use Sylius\Component\Mailer\Provider\EmailProviderInterface; use Sylius\Component\Mailer\Renderer\Adapter\AdapterInterface as RendererAdapterInterface; use Sylius\Component\Mailer\Renderer\RenderedEmail; -use Sylius\Component\Mailer\Sender\Adapter\AdapterInterface as SenderAdapterInterface; +use Sylius\Component\Mailer\Sender\Adapter\CcAwareAdapterInterface as SenderAdapterInterface; final class SenderSpec extends ObjectBehavior { @@ -57,11 +57,40 @@ function it_sends_an_email_through_the_adapter( $data, [], [], - ['cc@mail.com'], - ['bcc@mail.com'] )->shouldBeCalled(); - $this->send('bar', ['john@example.com'], $data, [], [], ['cc@mail.com'], ['bcc@mail.com']); + $this->send('bar', ['john@example.com'], $data, [], []); + } + + function it_sends_an_email_with_cc_and_bcc_through_the_adapter( + EmailInterface $email, + EmailProviderInterface $provider, + RenderedEmail $renderedEmail, + RendererAdapterInterface $rendererAdapter, + SenderAdapterInterface $senderAdapter, + ): void { + $provider->getEmail('bar')->willReturn($email); + $email->isEnabled()->willReturn(true); + $email->getSenderAddress()->willReturn('sender@example.com'); + $email->getSenderName()->willReturn('Sender'); + + $data = ['foo' => 2]; + + $rendererAdapter->render($email, ['foo' => 2])->willReturn($renderedEmail); + $senderAdapter->sendWithCC( + ['john@example.com'], + 'sender@example.com', + 'Sender', + $renderedEmail, + $email, + $data, + [], + [], + ['cc@example.com'], + ['bcc@example.com'], + )->shouldBeCalled(); + + $this->send('bar', ['john@example.com'], $data, [], [], ['cc@example.com'], ['bcc@example.com']); } function it_does_not_send_disabled_emails( From 8b9adbdd1a503432c2dfb5da11cb90e69b9cd8fc Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Thu, 23 Jun 2022 18:15:44 +0200 Subject: [PATCH 4/4] Deprecate uasge of SenderInterface without CC and BCC --- ecs.php | 2 ++ phpstan.neon | 2 ++ psalm.xml | 6 ++++++ src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php | 8 ++------ .../spec/Sender/Adapter/SymfonyMailerAdapterSpec.php | 2 +- src/Component/Sender/SenderInterface.php | 4 ++++ 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ecs.php b/ecs.php index 9dc643e..0d09b97 100644 --- a/ecs.php +++ b/ecs.php @@ -2,6 +2,7 @@ use PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer; use PhpCsFixer\Fixer\Comment\HeaderCommentFixer; +use PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer; use PhpCsFixer\Fixer\Phpdoc\PhpdocTagTypeFixer; use SlevomatCodingStandard\Sniffs\Commenting\InlineDocCommentDeclarationSniff; use Symplify\EasyCodingStandard\Config\ECSConfig; @@ -25,6 +26,7 @@ PhpdocTagTypeFixer::class, InlineDocCommentDeclarationSniff::class . '.MissingVariable', VisibilityRequiredFixer::class => ['*Spec.php'], + NoSuperfluousPhpdocTagsFixer::class => ['src/Component/Sender/SenderInterface.php'], '**/var/*', 'src/Bundle/test/app/AppKernel.php', ]); diff --git a/phpstan.neon b/phpstan.neon index c6517c5..5e1f7a9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -26,3 +26,5 @@ parameters: - '/Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters, 1 required\./' - '/Cannot call method has\(\) on object\|null/' - '/Property Sylius\\Component\\Mailer\\Model\\Email\:\:\$id is never written\, only read\./' + - '/PHPDoc tag \@param references unknown parameter\: \$bccRecipients/' + - '/PHPDoc tag \@param references unknown parameter\: \$ccRecipients/' diff --git a/psalm.xml b/psalm.xml index 47272cf..380a208 100644 --- a/psalm.xml +++ b/psalm.xml @@ -17,6 +17,12 @@ + + + + + + diff --git a/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php b/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php index 4ce5e36..eb256c1 100644 --- a/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php +++ b/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php @@ -103,12 +103,8 @@ private function sendMessage( ->html($renderedEmail->getBody()) ; - if (!empty($ccRecipients)) { - $message->addCc(...$ccRecipients); - } - if (!empty($bccRecipients)) { - $message->addBcc(...$bccRecipients); - } + $message->addCc(...$ccRecipients); + $message->addBcc(...$bccRecipients); foreach ($attachments as $attachment) { $message->attachFromPath($attachment); diff --git a/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php b/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php index fbe143c..80ac6dc 100644 --- a/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php +++ b/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php @@ -60,7 +60,7 @@ function it_sends_an_email_with_events( $message->getBody()->bodyToString() === 'body' && $message->getFrom()[0] == new Address('arnaud@sylius.com', 'arnaud') && $message->getTo()[0] == new Address('pawel@sylius.com') - ; + ; }))->shouldBeCalled(); $dispatcher diff --git a/src/Component/Sender/SenderInterface.php b/src/Component/Sender/SenderInterface.php index 184400c..62f29d4 100644 --- a/src/Component/Sender/SenderInterface.php +++ b/src/Component/Sender/SenderInterface.php @@ -16,9 +16,13 @@ interface SenderInterface { /** + * @deprecated using this method without 2 last arguments ($ccRecipients and $bccRecipients) is deprecated since 1.8 and won't be possible since 2.0 + * * @param string[]|null[] $recipients A list of email addresses to receive the message. Providing null or empty string in the list of recipients is deprecated and will be removed in SyliusMailerBundle 2.0 * @param string[] $attachments A list of file paths to attach to the message. * @param string[] $replyTo A list of email addresses to set as the Reply-To address for the message. + * @param string[] $ccRecipients A list of email addresses set as carbon copy + * @param string[] $bccRecipients A list of email addresses set as blind carbon copy */ public function send( string $code,