Skip to content

Commit 305dd97

Browse files
authored
Merge pull request #257 from presprog/email-presets
Support `preset` parameter in email action
2 parents bd383fc + ce6a518 commit 305dd97

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

docs/actions/email.md

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ return function ($kirby)
5252

5353
The email action accepts the same options than the [email function of Kirby](https://getkirby.com/docs/guide/emails). You can pass on options like `cc`, `bcc` or even `attachments`. The `body` is ignored, however, as it is dynamically generated based on the form data. Here are some special options:
5454

55+
5556
### to (required)
5657

5758
The email address that should be the receiver of the emails. It can be dynamically chosen based on the form content with the [EmailSelectAction](email-select).
@@ -60,6 +61,9 @@ The email address that should be the receiver of the emails. It can be dynamical
6061

6162
The email address that will be the sender of the emails. This should be some address that is associated with the website. If you host it at `example.com` the address may be `[email protected]`.
6263

64+
### preset
65+
The [Kirby email preset](https://getkirby.com/docs/guide/emails#email-presets) to use as a template. It works exactly like you pass in an preset to Kirbys own email function. Uniform uses the preset values as base and merges the action parameters with them. If you have `to` and `from` defined in your preset you do not have to pass them in as parameters again.
66+
6367
### subject
6468

6569
The subject of the email. By default the `uniform-email-subject` language variable is taken. The subject supports templates, too, so you can dynamically add form data to it. A template is a name of a form field surrounded by `{{}}`. Example:

src/Actions/EmailAction.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Kirby\Cms\App;
7+
use Kirby\Exception\NotFoundException;
78
use Kirby\Toolkit\Str;
89
use Kirby\Toolkit\I18n;
910

@@ -31,6 +32,8 @@ class EmailAction extends Action
3132
*/
3233
public function perform()
3334
{
35+
$this->options = $this->preset($this->option('preset'));
36+
3437
$params = array_merge($this->options, [
3538
'to' => $this->requireOption('to'),
3639
'from' => $this->requireOption('from'),
@@ -97,7 +100,7 @@ protected function handleException($e)
97100
*/
98101
protected function sendEmail(array $params)
99102
{
100-
App::instance()->email($params);
103+
App::instance()->email([], $params);
101104
}
102105

103106
/**
@@ -175,4 +178,29 @@ protected function shouldReceiveCopy()
175178
return $this->option('receive-copy') === true
176179
&& $this->form->data(self::RECEIVE_COPY_KEY);
177180
}
181+
182+
/**
183+
* Loads more options from Kirby email presets, if `preset` was set
184+
*
185+
* @return array
186+
*/
187+
private function preset(string|null $preset): array
188+
{
189+
if (!$preset) {
190+
return $this->options;
191+
}
192+
193+
if (($presetOptions = App::instance()->option('email.presets.' . $preset)) === null) {
194+
throw new NotFoundException([
195+
'key' => 'email.preset.notFound',
196+
'data' => ['name' => $preset],
197+
]);
198+
}
199+
200+
// Options passed to the action always superseed preset options
201+
$options = array_merge($presetOptions, $this->options);
202+
unset($options['preset']);
203+
204+
return $options;
205+
}
178206
}

tests/Actions/EmailActionTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Uniform\Tests\TestCase;
99
use Uniform\Actions\EmailAction;
1010
use Uniform\Exceptions\PerformerException;
11+
use Kirby\Exception\NotFoundException;
1112

1213
class EmailActionTest extends TestCase
1314
{
@@ -98,6 +99,46 @@ public function testPassthroughOptions()
9899
$this->assertEquals($expect, $action->params['data']);
99100
}
100101

102+
public function testEmailPresets()
103+
{
104+
App::instance()->extend([
105+
'options' => [
106+
'email' => [
107+
'presets' => [
108+
'default' => [
109+
'from' => '[email protected]',
110+
'fromName' => 'John Doe'
111+
],
112+
],
113+
],
114+
],
115+
]);
116+
117+
$this->form->data('message', 'hello');
118+
$action = new EmailActionStub($this->form, [
119+
'preset' => 'default',
120+
'to' => '[email protected]',
121+
'fromName' => 'Janet Doe'
122+
]);
123+
$action->perform();
124+
125+
$email = $action->email;
126+
$this->assertEquals('[email protected]', $email->from());
127+
$this->assertEquals('Janet Doe', $email->fromName());
128+
}
129+
130+
public function testEmailPresetNotDefined()
131+
{
132+
$action = new EmailActionStub($this->form, [
133+
'preset' => 'default',
134+
'to' => '[email protected]',
135+
'fromName' => 'Janet Doe'
136+
]);
137+
138+
$this->expectException(NotFoundException::class);
139+
$action->perform();
140+
}
141+
101142
public function testSubjectTemplate()
102143
{
103144
$this->form->data('email', "[email protected]");

0 commit comments

Comments
 (0)