Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify CC and BCC in sender #152

Merged
merged 4 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
]);
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
</projectFiles>

<issueHandlers>
<InvalidDocblockParamName>
<errorLevel type="suppress">
<file name="src/Component/Sender/SenderInterface.php" />
</errorLevel>
</InvalidDocblockParamName>

<PossiblyNullReference>
<errorLevel type="suppress">
<file name="src/Bundle/DependencyInjection/Configuration.php" />
Expand Down
18 changes: 18 additions & 0 deletions src/Bundle/Sender/Adapter/DefaultAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
}
}
60 changes: 59 additions & 1 deletion src/Bundle/Sender/Adapter/SwiftMailerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +56,56 @@ 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 \Swift_Message())
->setSubject($renderedEmail->getSubject())
Expand All @@ -63,6 +114,13 @@ public function send(
->setReplyTo($replyTo)
;

if (!empty($ccRecipients)) {
$message->setCc($ccRecipients);
}
if (!empty($bccRecipients)) {
$message->setBcc($bccRecipients);
}

$message->setBody($renderedEmail->getBody(), 'text/html');

foreach ($attachments as $attachment) {
Expand Down
56 changes: 55 additions & 1 deletion src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -43,6 +44,56 @@ 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())
Expand All @@ -52,6 +103,9 @@ public function send(
->html($renderedEmail->getBody())
;

$message->addCc(...$ccRecipients);
$message->addBcc(...$bccRecipients);

foreach ($attachments as $attachment) {
$message->attachFromPath($attachment);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@ function it_throws_an_exception_about_not_configured_email_sender_adapter(
)))
->during('send', [['[email protected]'], '[email protected]', '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', [['[email protected]'], '[email protected]', 'arnaud', $renderedEmail, $email, [], [], [], ['[email protected]'], ['[email protected]']])
;
}
}
55 changes: 54 additions & 1 deletion src/Bundle/spec/Sender/Adapter/SwiftMailerAdapterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() === ['[email protected]' => 'arnaud'] &&
$message->getTo() === ['[email protected]' => null]
;
}))->shouldBeCalled();

$dispatcher
->dispatch(Argument::type(EmailSendEvent::class), SyliusMailerEvents::EMAIL_POST_SEND)
Expand All @@ -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() === ['[email protected]' => 'arnaud'] &&
$message->getTo() === ['[email protected]' => null] &&
$message->getCc() === ['[email protected]' => null] &&
$message->getBcc() === ['[email protected]' => null]
;
}))->shouldBeCalled();

$dispatcher
->dispatch(Argument::type(EmailSendEvent::class), SyliusMailerEvents::EMAIL_POST_SEND)
->shouldBeCalled()
;

$this->sendWithCC(
['[email protected]'],
'[email protected]',
'arnaud',
$renderedEmail,
$email,
[],
[],
[],
['[email protected]'],
['[email protected]'],
);
}
}
43 changes: 42 additions & 1 deletion src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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('[email protected]', 'arnaud') &&
$message->getTo()[0] == new Address('[email protected]')
;
}))->shouldBeCalled();

$dispatcher
->dispatch(Argument::type(EmailSendEvent::class), SyliusMailerEvents::EMAIL_POST_SEND)
Expand All @@ -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('[email protected]', 'arnaud') &&
$message->getTo()[0] == new Address('[email protected]') &&
$message->getCc()[0] == new Address('[email protected]') &&
$message->getBcc()[0] == new Address('[email protected]')
;
}))->shouldBeCalled();

$this->sendWithCC(
['[email protected]'],
'[email protected]',
'arnaud',
$renderedEmail,
$email,
[],
[],
[],
['[email protected]'],
['[email protected]'],
);
}

function it_sends_an_email_with_attachments(
MailerInterface $mailer,
EmailInterface $email,
Expand Down
Loading