Skip to content

Commit

Permalink
Merge pull request #243 from arnoson/master
Browse files Browse the repository at this point in the history
Allow body in email action
  • Loading branch information
mzur authored Mar 15, 2023
2 parents 5a2ab95 + f7727e2 commit bc8a691
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
14 changes: 13 additions & 1 deletion docs/actions/email.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Email Action

This actions sends the form data by email. In its simplest form it just appends all form fields in `name: value` pairs as plain text. But it can use a [snippet](#snippet) to build the email, too. You can use snippets to send HTML instead of plain text emails, too.
This actions sends the form data by email. In its simplest form it just appends all form fields in `name: value` pairs as plain text. But it can use a custom [body](#body) or [template](#template) to build the email, too. You can use templates to send HTML instead of plain text emails, too.

If there is an `email` field in the form data, the action will use it as `replyTo` of the sent email and remove it from the email body. If there is a `receive_copy` field present (e.g. a checkbox) and the [receive-copy](#receive-copy) option is set, the action will send a copy of the email to the address specified in the `email` field. The subject of this copy email will get the `uniform-email-copy` prefix.

Expand Down Expand Up @@ -80,6 +80,18 @@ Check out the email templates of the [Uniform repo](https://github.com/mzur/kirb
!!! warning "Note"
You cannot access form fields with the name `_data` or `_options` directly in the template as these are reserved for the additional variables provided by Uniform. Use `$_data['_data']` and `$_data['_options']` in this case.

### body

The body of the email. If not specified, the form data will be used as the body (`name: value` pairs as plain text). The body supports templates, too, so you can dynamically add form data to it. A template is a name of a form field surrounded by `{{}}`. Example:

```php
'body' => 'Dear {{name}}, we will get back to you soon!',
```
The body will only be used, if no [template](#template) is specified.

!!! warning "Note"
Body templates do not work with [array form fields](http://stackoverflow.com/a/1978788/1796523).

### replyTo

Set a static email address as `replyTo` of the email instead of the value of the `email` form field.
Expand Down
25 changes: 19 additions & 6 deletions src/Actions/EmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Uniform\Actions;

use Exception;
use Uniform\Form;
use Kirby\Cms\App;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\I18n;
Expand Down Expand Up @@ -56,6 +55,8 @@ public function perform()
'_data' => $params['data'],
'_options' => $this->options,
]);
} else if (isset($params['body']) && is_string($params['body'])) {
$params['body'] = $this->resolveTemplate($params['body']);
} else {
$params['body'] = $this->getBody($this->form->data('', '', $escape));
}
Expand Down Expand Up @@ -100,12 +101,13 @@ protected function sendEmail(array $params)
}

/**
* Get the email subject and resolve possible template strings
*
* Resolve template strings
*
* @param string $string
*
* @return string
*/
protected function getSubject()
{
protected function resolveTemplate($string) {
// the form could contain arrays which are incompatible with the template function
$templatableItems = array_filter($this->form->data(), function ($item) {
return is_scalar($item);
Expand All @@ -119,8 +121,18 @@ protected function getSubject()
$fallback = '';
}

$subject = Str::template($this->option('subject', I18n::translate('uniform-email-subject')), $templatableItems, $fallback);
return Str::template($string, $templatableItems, $fallback);
}

/**
* Get the email subject and resolve possible template strings
*
* @return string
*/
protected function getSubject()
{
$subject = $this->resolveTemplate($this->option('subject', I18n::translate('uniform-email-subject')));

// Remove newlines to prevent malicious modifications of the email header.
return str_replace("\n", '', $subject);
}
Expand All @@ -136,6 +148,7 @@ protected function getBody($data)
{
unset($data[self::EMAIL_KEY]);
unset($data[self::RECEIVE_COPY_KEY]);

$body = '';
foreach ($data as $key => $value) {
if (is_array($value)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Actions/EmailActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ public function testBody()
$this->assertEquals($expect, $action->email->body()->text());
}

public function testBodyTemplate()
{
$this->form->data('email', '[email protected]');
$this->form->data('name', 'Joe');
$this->form->data('data', ['somedata']);
$action = new EmailActionStub($this->form, [
'to' => '[email protected]',
'from' => '[email protected]',
'body' => "Hello\n{{name}} with {{data}}"
]);
$action->perform();
$this->assertEquals("Hello\nJoe with ", $action->email->body()->text());
}

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

0 comments on commit bc8a691

Please sign in to comment.