Skip to content

Commit

Permalink
Merge pull request #257 from presprog/email-presets
Browse files Browse the repository at this point in the history
Support `preset` parameter in email action
  • Loading branch information
mzur authored Feb 21, 2024
2 parents bd383fc + ce6a518 commit 305dd97
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/actions/email.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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 `[email protected]`.

### 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:
Expand Down
30 changes: 29 additions & 1 deletion src/Actions/EmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Kirby\Cms\App;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\I18n;

Expand Down Expand Up @@ -31,6 +32,8 @@ class EmailAction extends Action
*/
public function perform()
{
$this->options = $this->preset($this->option('preset'));

$params = array_merge($this->options, [
'to' => $this->requireOption('to'),
'from' => $this->requireOption('from'),
Expand Down Expand Up @@ -97,7 +100,7 @@ protected function handleException($e)
*/
protected function sendEmail(array $params)
{
App::instance()->email($params);
App::instance()->email([], $params);
}

/**
Expand Down Expand Up @@ -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;
}
}
41 changes: 41 additions & 0 deletions tests/Actions/EmailActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Uniform\Tests\TestCase;
use Uniform\Actions\EmailAction;
use Uniform\Exceptions\PerformerException;
use Kirby\Exception\NotFoundException;

class EmailActionTest extends TestCase
{
Expand Down Expand Up @@ -98,6 +99,46 @@ public function testPassthroughOptions()
$this->assertEquals($expect, $action->params['data']);
}

public function testEmailPresets()
{
App::instance()->extend([
'options' => [
'email' => [
'presets' => [
'default' => [
'from' => '[email protected]',
'fromName' => 'John Doe'
],
],
],
],
]);

$this->form->data('message', 'hello');
$action = new EmailActionStub($this->form, [
'preset' => 'default',
'to' => '[email protected]',
'fromName' => 'Janet Doe'
]);
$action->perform();

$email = $action->email;
$this->assertEquals('[email protected]', $email->from());
$this->assertEquals('Janet Doe', $email->fromName());
}

public function testEmailPresetNotDefined()
{
$action = new EmailActionStub($this->form, [
'preset' => 'default',
'to' => '[email protected]',
'fromName' => 'Janet Doe'
]);

$this->expectException(NotFoundException::class);
$action->perform();
}

public function testSubjectTemplate()
{
$this->form->data('email', "[email protected]");
Expand Down

0 comments on commit 305dd97

Please sign in to comment.