From 81d13fd931e3f30f01fbdea4e845bef607693552 Mon Sep 17 00:00:00 2001 From: Benedict Massolle Date: Fri, 16 Feb 2024 10:07:48 +0100 Subject: [PATCH 1/4] Support `preset` parameter in email action --- src/Actions/EmailAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Actions/EmailAction.php b/src/Actions/EmailAction.php index 5999844..21b00c0 100644 --- a/src/Actions/EmailAction.php +++ b/src/Actions/EmailAction.php @@ -97,7 +97,7 @@ protected function handleException($e) */ protected function sendEmail(array $params) { - App::instance()->email($params); + App::instance()->email($params['preset'] ?? [], $params); } /** From 06746ff947be6f555f9465b2136224c2d51c1aba Mon Sep 17 00:00:00 2001 From: Benedict Massolle Date: Tue, 20 Feb 2024 08:35:23 +0100 Subject: [PATCH 2/4] Refactor preset handling, add test cases --- src/Actions/EmailAction.php | 30 +++++++++++++++++++++- tests/Actions/EmailActionTest.php | 41 +++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/Actions/EmailAction.php b/src/Actions/EmailAction.php index 21b00c0..44d343a 100644 --- a/src/Actions/EmailAction.php +++ b/src/Actions/EmailAction.php @@ -4,6 +4,7 @@ use Exception; use Kirby\Cms\App; +use Kirby\Exception\NotFoundException; use Kirby\Toolkit\Str; use Kirby\Toolkit\I18n; @@ -31,6 +32,8 @@ class EmailAction extends Action */ public function perform() { + $this->options = $this->preset($this->options['preset'] ?? null); + $params = array_merge($this->options, [ 'to' => $this->requireOption('to'), 'from' => $this->requireOption('from'), @@ -97,7 +100,7 @@ protected function handleException($e) */ protected function sendEmail(array $params) { - App::instance()->email($params['preset'] ?? [], $params); + App::instance()->email([], $params); } /** @@ -175,4 +178,29 @@ protected function shouldReceiveCopy() return $this->option('receive-copy') === true && $this->form->data(self::RECEIVE_COPY_KEY); } + + /** + * Loads more options from Kirby email presets, if `preset` was set + * + * @return array + */ + private function preset(string|null $preset): array + { + if (!$preset) { + return $this->options; + } + + if (($presetOptions = App::instance()->option('email.presets.' . $preset)) === null) { + throw new NotFoundException([ + 'key' => 'email.preset.notFound', + 'data' => ['name' => $preset], + ]); + } + + // Options passed to the action always superseed preset options + $options = array_merge($presetOptions, $this->options); + unset($options['preset']); + + return $options; + } } diff --git a/tests/Actions/EmailActionTest.php b/tests/Actions/EmailActionTest.php index a342dce..8d9ffc5 100644 --- a/tests/Actions/EmailActionTest.php +++ b/tests/Actions/EmailActionTest.php @@ -8,6 +8,7 @@ use Uniform\Tests\TestCase; use Uniform\Actions\EmailAction; use Uniform\Exceptions\PerformerException; +use Kirby\Exception\NotFoundException; class EmailActionTest extends TestCase { @@ -98,6 +99,46 @@ public function testPassthroughOptions() $this->assertEquals($expect, $action->params['data']); } + public function testEmailPresets() + { + App::instance()->extend([ + 'options' => [ + 'email' => [ + 'presets' => [ + 'default' => [ + 'from' => 'john@user.com', + 'fromName' => 'John Doe' + ], + ], + ], + ], + ]); + + $this->form->data('message', 'hello'); + $action = new EmailActionStub($this->form, [ + 'preset' => 'default', + 'to' => 'jane@user.com', + 'fromName' => 'Janet Doe' + ]); + $action->perform(); + + $email = $action->email; + $this->assertEquals('john@user.com', $email->from()); + $this->assertEquals('Janet Doe', $email->fromName()); + } + + public function testEmailPresetNotDefined() + { + $action = new EmailActionStub($this->form, [ + 'preset' => 'default', + 'to' => 'janet@user.com', + 'fromName' => 'Janet Doe' + ]); + + $this->expectException(NotFoundException::class); + $action->perform(); + } + public function testSubjectTemplate() { $this->form->data('email', "joe@user.com"); From 8f9abf1286c34441840443b8e3ff30fee9360c6d Mon Sep 17 00:00:00 2001 From: Benedict Massolle Date: Wed, 21 Feb 2024 08:27:14 +0100 Subject: [PATCH 3/4] Use option() method to get preset option --- src/Actions/EmailAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Actions/EmailAction.php b/src/Actions/EmailAction.php index 44d343a..734be68 100644 --- a/src/Actions/EmailAction.php +++ b/src/Actions/EmailAction.php @@ -32,7 +32,7 @@ class EmailAction extends Action */ public function perform() { - $this->options = $this->preset($this->options['preset'] ?? null); + $this->options = $this->preset($this->option('preset')); $params = array_merge($this->options, [ 'to' => $this->requireOption('to'), From ce6a518b251882400aae11af5da8d97b4ec1134e Mon Sep 17 00:00:00 2001 From: Benedict Massolle Date: Wed, 21 Feb 2024 08:33:21 +0100 Subject: [PATCH 4/4] Update docs --- docs/actions/email.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/actions/email.md b/docs/actions/email.md index b50a9e7..2b8147e 100644 --- a/docs/actions/email.md +++ b/docs/actions/email.md @@ -52,6 +52,7 @@ return function ($kirby) 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: + ### to (required) 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 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 `info@example.com`. +### preset +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. + ### subject 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: