Skip to content

Commit b116e9e

Browse files
author
arnoson
committed
Allow body in email action
1 parent 5a2ab95 commit b116e9e

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/Actions/EmailAction.php

+22-7
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;
@@ -100,12 +99,13 @@ protected function sendEmail(array $params)
10099
}
101100

102101
/**
103-
* Get the email subject and resolve possible template strings
104-
*
102+
* Resolve template strings
103+
*
104+
* @param string $string
105+
*
105106
* @return string
106107
*/
107-
protected function getSubject()
108-
{
108+
protected function resolveTemplate($string) {
109109
// the form could contain arrays which are incompatible with the template function
110110
$templatableItems = array_filter($this->form->data(), function ($item) {
111111
return is_scalar($item);
@@ -119,14 +119,24 @@ protected function getSubject()
119119
$fallback = '';
120120
}
121121

122-
$subject = Str::template($this->option('subject', I18n::translate('uniform-email-subject')), $templatableItems, $fallback);
122+
return Str::template($string, $templatableItems, $fallback);
123+
}
124+
125+
/**
126+
* Get the email subject and resolve possible template strings
127+
*
128+
* @return string
129+
*/
130+
protected function getSubject()
131+
{
132+
$subject = $this->resolveTemplate($this->option('subject', I18n::translate('uniform-email-subject')));
123133

124134
// Remove newlines to prevent malicious modifications of the email header.
125135
return str_replace("\n", '', $subject);
126136
}
127137

128138
/**
129-
* Get the email body
139+
* Get the email body and resolve possible template strings
130140
*
131141
* @param array $data
132142
*
@@ -136,6 +146,11 @@ protected function getBody($data)
136146
{
137147
unset($data[self::EMAIL_KEY]);
138148
unset($data[self::RECEIVE_COPY_KEY]);
149+
150+
if (isset($data['body'])) {
151+
return $this->resolveTemplate($data['body']);
152+
}
153+
139154
$body = '';
140155
foreach ($data as $key => $value) {
141156
if (is_array($value)) {

tests/Actions/EmailActionTest.php

+14
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', '[email protected]');
133+
$this->form->data('name', 'Joe');
134+
$this->form->data('data', ['somedata']);
135+
$action = new EmailActionStub($this->form, [
136+
'to' => '[email protected]',
137+
'from' => '[email protected]',
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', '[email protected]');

0 commit comments

Comments
 (0)