Skip to content

Commit 81f94c3

Browse files
jasonvargaclaude
andauthored
[6.x] Forms 2: Pass email addresses to custom mail jobs as strings (#16020)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b6bf2ae commit 81f94c3

6 files changed

Lines changed: 158 additions & 3 deletions

File tree

‎src/Forms/Connectors/Email.php‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,19 @@ protected function job(Submission $submission, array $connection): ?object
3939
{
4040
$class = config('statamic.forms.send_email_job');
4141

42-
return new $class($submission, $submission->site(), $connection);
42+
return new $class($submission, $submission->site(), static::stringifyAddresses($connection));
43+
}
44+
45+
/** @internal */
46+
public static function stringifyAddresses(array $connection): array
47+
{
48+
foreach (['to', 'cc', 'bcc', 'reply_to'] as $handle) {
49+
if (isset($connection[$handle]) && is_array($connection[$handle])) {
50+
$connection[$handle] = implode(', ', $connection[$handle]);
51+
}
52+
}
53+
54+
return $connection;
4355
}
4456

4557
public function render(): VueComponent
@@ -59,13 +71,56 @@ private function convertLegacyAddresses(array $config): array
5971
{
6072
foreach (['to', 'cc', 'bcc', 'reply_to'] as $handle) {
6173
if (isset($config[$handle]) && is_string($config[$handle])) {
62-
$config[$handle] = array_map(trim(...), explode(',', $config[$handle]));
74+
$config[$handle] = array_map(trim(...), $this->splitAddresses($config[$handle]));
6375
}
6476
}
6577

6678
return $config;
6779
}
6880

81+
private function splitAddresses(string $value): array
82+
{
83+
$parts = [];
84+
$current = '';
85+
$depth = 0;
86+
$quote = null;
87+
88+
for ($i = 0; $i < strlen($value); $i++) {
89+
$char = $value[$i];
90+
$pair = substr($value, $i, 2);
91+
92+
if ($quote) {
93+
if ($char === '\\') {
94+
$current .= $pair;
95+
$i++;
96+
97+
continue;
98+
}
99+
100+
if ($char === $quote) {
101+
$quote = null;
102+
}
103+
} elseif ($pair === '{{' || ($pair === '}}' && $depth > 0)) {
104+
$depth += $pair === '{{' ? 1 : -1;
105+
$current .= $pair;
106+
$i++;
107+
108+
continue;
109+
} elseif ($depth > 0 && ($char === '"' || $char === "'")) {
110+
$quote = $char;
111+
} elseif ($char === ',' && $depth === 0) {
112+
$parts[] = $current;
113+
$current = '';
114+
115+
continue;
116+
}
117+
118+
$current .= $char;
119+
}
120+
121+
return [...$parts, $current];
122+
}
123+
69124
protected function connectionRules(): array
70125
{
71126
$templateSyntax = new EmailConnectionTemplateSyntax($this->form());

‎src/Forms/Email.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ protected function addresses($addresses)
319319
}
320320

321321
return collect(Arr::wrap($addresses))
322+
->flatMap(fn ($address) => is_scalar($address) ? explode(',', (string) $address) : [])
323+
->map(fn ($address) => trim($address))
322324
->flatMap(fn ($address) => Str::startsWith($address, 'field:')
323325
? Arr::wrap($this->submission->get(Str::after($address, 'field:')))
324326
: [$address])

‎src/Forms/SendEmails.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Statamic\Contracts\Forms\Submission;
1010
use Statamic\Fields\Field;
1111
use Statamic\Forms\Connectors\ConnectionLogic;
12+
use Statamic\Forms\Connectors\Email as EmailConnector;
1213
use Statamic\Sites\Site;
1314

1415
/**
@@ -34,7 +35,7 @@ public function handle(): void
3435

3536
$jobs = collect($emailConfigs)
3637
->filter(fn (array $config) => ConnectionLogic::passes($config, $submission))
37-
->map(fn (array $config) => new $class($submission, $this->site, $config))
38+
->map(fn (array $config) => new $class($submission, $this->site, EmailConnector::stringifyAddresses($config)))
3839
->values()
3940
->all();
4041

‎tests/Forms/Connectors/EmailConnectorTest.php‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,46 @@ public function it_uses_the_configured_send_email_job()
6060
$this->assertContainsOnlyInstancesOf(CustomSendEmail::class, $jobs);
6161
}
6262

63+
#[Test]
64+
#[DataProvider('sendEmailJobProvider')]
65+
public function it_passes_addresses_to_the_job_as_comma_separated_strings(string $class)
66+
{
67+
config(['statamic.forms.send_email_job' => $class]);
68+
69+
$form = tap(Form::make('test')->connections(['email' => [
70+
[
71+
'id' => 'one',
72+
'to' => ['field:email', 'first@example.com'],
73+
'cc' => ['cc@example.com'],
74+
'bcc' => ['{{ x ?? "a@example.com, b@example.com" }}', 'bcc@example.com'],
75+
'reply_to' => ['field:email'],
76+
'from' => 'sender@example.com',
77+
],
78+
['id' => 'two', 'to' => 'legacy@example.com, other@example.com'],
79+
]]))->save();
80+
81+
$jobs = (new Email)->forForm($form)->finalized($form->makeSubmission());
82+
83+
$this->assertContainsOnlyInstancesOf($class, $jobs);
84+
$this->assertSame([
85+
'id' => 'one',
86+
'to' => 'field:email, first@example.com',
87+
'cc' => 'cc@example.com',
88+
'bcc' => '{{ x ?? "a@example.com, b@example.com" }}, bcc@example.com',
89+
'reply_to' => 'field:email',
90+
'from' => 'sender@example.com',
91+
], $jobs[0]->config);
92+
$this->assertSame(['id' => 'two', 'to' => 'legacy@example.com, other@example.com'], $jobs[1]->config);
93+
}
94+
95+
public static function sendEmailJobProvider(): array
96+
{
97+
return [
98+
'core job' => [SendEmail::class],
99+
'custom job' => [CustomSendEmail::class],
100+
];
101+
}
102+
63103
#[Test]
64104
public function it_filters_emails_using_conditions_when_finalized()
65105
{
@@ -573,6 +613,22 @@ public function it_converts_legacy_address_strings_into_arrays(string $handle)
573613
$this->assertEquals(['first@example.com', 'second@example.com'], $configs[0][$handle]);
574614
}
575615

616+
#[Test]
617+
public function it_doesnt_split_legacy_address_strings_inside_antlers()
618+
{
619+
$form = tap(Form::make('test')->connections(['email' => [
620+
['id' => 'one', 'to' => '{{ x ?? "a@example.com, b@example.com" }}, {{ partial:foo a=\'1,2\' }}, c@example.com'],
621+
]]))->save();
622+
623+
$configs = (new Email)->setForm($form)->preProcess($form->connections()->get('email'));
624+
625+
$this->assertEquals([
626+
'{{ x ?? "a@example.com, b@example.com" }}',
627+
"{{ partial:foo a='1,2' }}",
628+
'c@example.com',
629+
], $configs[0]['to']);
630+
}
631+
576632
#[Test]
577633
public function legacy_address_strings_are_converted_before_meta_is_built()
578634
{

‎tests/Forms/EmailTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ public static function multipleAddressProvider()
163163
'array with a field reference to a missing field' => [['field:nonexistent', 'foo@bar.com'], [
164164
['address' => 'foo@bar.com', 'name' => null],
165165
]],
166+
'string with a field reference and an email' => ['field:email, baz@qux.com', [
167+
['address' => 'foo@bar.com', 'name' => null],
168+
['address' => 'baz@qux.com', 'name' => null],
169+
]],
170+
'string with an email and a field reference' => ['baz@qux.com,field:email', [
171+
['address' => 'baz@qux.com', 'name' => null],
172+
['address' => 'foo@bar.com', 'name' => null],
173+
]],
174+
'string with a field reference to a missing field' => ['field:nonexistent, foo@bar.com', [
175+
['address' => 'foo@bar.com', 'name' => null],
176+
]],
166177
]);
167178
}
168179

‎tests/Forms/SendEmailsTest.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,36 @@ public function it_sends_emails_without_a_to_address_when_cc_or_bcc_resolves($ke
171171
Mail::assertSent(Email::class, 1);
172172
}
173173

174+
#[Test]
175+
public function it_passes_array_addresses_to_the_job_as_comma_separated_strings()
176+
{
177+
Mail::fake();
178+
179+
$form = tap(FacadesForm::make('test')->formFields([
180+
'fields' => [
181+
['handle' => 'email', 'field' => ['type' => 'text']],
182+
],
183+
])->connections(['email' => [
184+
[
185+
'id' => 'one',
186+
'to' => ['field:email', 'first@example.com'],
187+
'cc' => ['cc@example.com'],
188+
'bcc' => ['bcc@example.com'],
189+
'reply_to' => ['field:email'],
190+
],
191+
]]))->save();
192+
193+
$this->sendEmails($form->makeSubmission()->data(['email' => 'someone@example.com']));
194+
195+
Mail::assertSent(Email::class, fn (Email $email) => $email->getConfig() === [
196+
'id' => 'one',
197+
'to' => 'field:email, first@example.com',
198+
'cc' => 'cc@example.com',
199+
'bcc' => 'bcc@example.com',
200+
'reply_to' => 'field:email',
201+
] && collect(tap($email)->build()->to)->pluck('address')->all() === ['someone@example.com', 'first@example.com']);
202+
}
203+
174204
public static function ccAndBccProvider()
175205
{
176206
return [

0 commit comments

Comments
 (0)