Skip to content

Commit 3c86d2f

Browse files
authored
Merge branch '2.1' into dependabot/composer/phpstan/phpstan-phpunit-1.4.0
2 parents ab21af3 + 0c2df3e commit 3c86d2f

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"require-dev": {
4141
"matthiasnoback/symfony-dependency-injection-test": "^5.0",
4242
"phpspec/phpspec": "^7.0",
43-
"phpstan/phpstan": "1.11.8",
43+
"phpstan/phpstan": "1.12.4",
4444
"phpstan/phpstan-phpunit": "1.4.0",
4545
"phpstan/phpstan-webmozart-assert": "1.2.0",
4646
"phpunit/phpunit": "^10.5",

src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace Sylius\Bundle\MailerBundle\Sender\Adapter;
1515

16+
use Egulias\EmailValidator\EmailValidator;
17+
use Egulias\EmailValidator\Validation\RFCValidation;
1618
use Sylius\Component\Mailer\Event\EmailSendEvent;
1719
use Sylius\Component\Mailer\Model\EmailInterface;
1820
use Sylius\Component\Mailer\Renderer\RenderedEmail;
@@ -99,13 +101,13 @@ private function sendMessage(
99101
$message = (new Email())
100102
->subject($renderedEmail->getSubject())
101103
->from(new Address($senderAddress, $senderName))
102-
->to(...$recipients)
104+
->to(...$this->formatRecipients($recipients))
103105
->replyTo(...$replyTo)
104106
->html($renderedEmail->getBody())
105107
;
106108

107-
$message->addCc(...$ccRecipients);
108-
$message->addBcc(...$bccRecipients);
109+
$message->addCc(...$this->formatRecipients($ccRecipients));
110+
$message->addBcc(...$this->formatRecipients($bccRecipients));
109111

110112
foreach ($attachments as $attachment) {
111113
$message->attachFromPath($attachment);
@@ -119,4 +121,29 @@ private function sendMessage(
119121

120122
$this->dispatcher?->dispatch($emailSendEvent, SyliusMailerEvents::EMAIL_POST_SEND);
121123
}
124+
125+
/**
126+
* There are two kinds of recipient array syntax that can be passed to the send method:
127+
* - Only email addresses: ['john.doe@mail.com', 'jane.smith@mail.com']
128+
* - Email addresses with names: ['john.doe@mail.com' => 'John Doe', 'jane.smith@mail.com' => 'Jane Smith']
129+
*
130+
* Since Symfony\Mailer requires an instance of Symfony\Component\Mime\Address or a valid email string for each recipient,
131+
* we need to transform the recipient array where keys are address and values are name to an array of Address object.
132+
*/
133+
protected function formatRecipients(array $recipients): array
134+
{
135+
$transformedRecipients = [];
136+
$validator = new EmailValidator();
137+
foreach ($recipients as $addressOrKey => $nameOrAddress) {
138+
if (\is_string($addressOrKey) && $validator->isValid($addressOrKey, new RFCValidation())) {
139+
$transformedRecipients[] = new Address($addressOrKey, $nameOrAddress);
140+
141+
continue;
142+
}
143+
144+
$transformedRecipients[] = $nameOrAddress;
145+
}
146+
147+
return $transformedRecipients;
148+
}
122149
}

src/Component/spec/Sender/SenderSpec.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,35 @@ function it_sends_an_email_through_the_adapter(
6363
$this->send('bar', ['john@example.com'], $data, [], []);
6464
}
6565

66+
function it_sends_an_email_and_name_pair_through_the_adapter(
67+
EmailInterface $email,
68+
EmailProviderInterface $provider,
69+
RenderedEmail $renderedEmail,
70+
RendererAdapterInterface $rendererAdapter,
71+
SenderAdapterInterface $senderAdapter,
72+
): void {
73+
$provider->getEmail('bar')->willReturn($email);
74+
$email->isEnabled()->willReturn(true);
75+
$email->getSenderAddress()->willReturn('sender@example.com');
76+
$email->getSenderName()->willReturn('Sender');
77+
78+
$data = ['foo' => 2];
79+
80+
$rendererAdapter->render($email, ['foo' => 2])->willReturn($renderedEmail);
81+
$senderAdapter->send(
82+
['john@example.com' => 'John Doe'],
83+
'sender@example.com',
84+
'Sender',
85+
$renderedEmail,
86+
$email,
87+
$data,
88+
[],
89+
[],
90+
)->shouldBeCalled();
91+
92+
$this->send('bar', ['john@example.com' => 'John Doe'], $data, [], []);
93+
}
94+
6695
function it_sends_an_email_with_cc_and_bcc_through_the_adapter(
6796
EmailInterface $email,
6897
EmailProviderInterface $provider,
@@ -94,6 +123,37 @@ function it_sends_an_email_with_cc_and_bcc_through_the_adapter(
94123
$this->send('bar', ['john@example.com'], $data, [], [], ['cc@example.com'], ['bcc@example.com']);
95124
}
96125

126+
function it_sends_an_email_with_cc_and_name_pair_and_bcc_and_name_pair_through_the_adapter(
127+
EmailInterface $email,
128+
EmailProviderInterface $provider,
129+
RenderedEmail $renderedEmail,
130+
RendererAdapterInterface $rendererAdapter,
131+
SenderAdapterInterface $senderAdapter,
132+
): void {
133+
$provider->getEmail('bar')->willReturn($email);
134+
$email->isEnabled()->willReturn(true);
135+
$email->getSenderAddress()->willReturn('sender@example.com');
136+
$email->getSenderName()->willReturn('Sender');
137+
138+
$data = ['foo' => 2];
139+
140+
$rendererAdapter->render($email, ['foo' => 2])->willReturn($renderedEmail);
141+
$senderAdapter->sendWithCC(
142+
['john@example.com'],
143+
'sender@example.com',
144+
'Sender',
145+
$renderedEmail,
146+
$email,
147+
$data,
148+
[],
149+
[],
150+
['cc@example.com' => 'CC'],
151+
['bcc@example.com' => 'BCC'],
152+
)->shouldBeCalled();
153+
154+
$this->send('bar', ['john@example.com'], $data, [], [], ['cc@example.com' => 'CC'], ['bcc@example.com' => 'BCC']);
155+
}
156+
97157
function it_sends_a_modified_email_with_cc_and_bcc_through_the_adapter(
98158
EmailInterface $email,
99159
EmailProviderInterface $provider,

0 commit comments

Comments
 (0)