Skip to content

Commit bc8a691

Browse files
authored
Merge pull request #243 from arnoson/master
Allow body in email action
2 parents 5a2ab95 + f7727e2 commit bc8a691

3 files changed

Lines changed: 46 additions & 7 deletions

File tree

docs/actions/email.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Email Action
22

3-
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.
3+
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.
44

55
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.
66

@@ -80,6 +80,18 @@ Check out the email templates of the [Uniform repo](https://github.com/mzur/kirb
8080
!!! warning "Note"
8181
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.
8282

83+
### body
84+
85+
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:
86+
87+
```php
88+
'body' => 'Dear {{name}}, we will get back to you soon!',
89+
```
90+
The body will only be used, if no [template](#template) is specified.
91+
92+
!!! warning "Note"
93+
Body templates do not work with [array form fields](http://stackoverflow.com/a/1978788/1796523).
94+
8395
### replyTo
8496

8597
Set a static email address as `replyTo` of the email instead of the value of the `email` form field.

src/Actions/EmailAction.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Uniform\Actions;
44

55
use Exception;
6-
use Uniform\Form;
76
use Kirby\Cms\App;
87
use Kirby\Toolkit\Str;
98
use Kirby\Toolkit\I18n;
@@ -56,6 +55,8 @@ public function perform()
5655
'_data' => $params['data'],
5756
'_options' => $this->options,
5857
]);
58+
} else if (isset($params['body']) && is_string($params['body'])) {
59+
$params['body'] = $this->resolveTemplate($params['body']);
5960
} else {
6061
$params['body'] = $this->getBody($this->form->data('', '', $escape));
6162
}
@@ -100,12 +101,13 @@ protected function sendEmail(array $params)
100101
}
101102

102103
/**
103-
* Get the email subject and resolve possible template strings
104-
*
104+
* Resolve template strings
105+
*
106+
* @param string $string
107+
*
105108
* @return string
106109
*/
107-
protected function getSubject()
108-
{
110+
protected function resolveTemplate($string) {
109111
// the form could contain arrays which are incompatible with the template function
110112
$templatableItems = array_filter($this->form->data(), function ($item) {
111113
return is_scalar($item);
@@ -119,8 +121,18 @@ protected function getSubject()
119121
$fallback = '';
120122
}
121123

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

127+
/**
128+
* Get the email subject and resolve possible template strings
129+
*
130+
* @return string
131+
*/
132+
protected function getSubject()
133+
{
134+
$subject = $this->resolveTemplate($this->option('subject', I18n::translate('uniform-email-subject')));
135+
124136
// Remove newlines to prevent malicious modifications of the email header.
125137
return str_replace("\n", '', $subject);
126138
}
@@ -136,6 +148,7 @@ protected function getBody($data)
136148
{
137149
unset($data[self::EMAIL_KEY]);
138150
unset($data[self::RECEIVE_COPY_KEY]);
151+
139152
$body = '';
140153
foreach ($data as $key => $value) {
141154
if (is_array($value)) {

tests/Actions/EmailActionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ public function testBody()
127127
$this->assertEquals($expect, $action->email->body()->text());
128128
}
129129

130+
public function testBodyTemplate()
131+
{
132+
$this->form->data('email', 'joe@user.com');
133+
$this->form->data('name', 'Joe');
134+
$this->form->data('data', ['somedata']);
135+
$action = new EmailActionStub($this->form, [
136+
'to' => 'jane@user.com',
137+
'from' => 'info@user.com',
138+
'body' => "Hello\n{{name}} with {{data}}"
139+
]);
140+
$action->perform();
141+
$this->assertEquals("Hello\nJoe with ", $action->email->body()->text());
142+
}
143+
130144
public function testBodyEscapeHtml()
131145
{
132146
$this->form->data('email', 'joe@user.com');

0 commit comments

Comments
 (0)