diff --git a/code/Control/UserDefinedFormController.php b/code/Control/UserDefinedFormController.php index 1e73ad6..7d1b296 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -345,6 +345,8 @@ JS // Add specific template data for the current recipient $emailData['HideFormData'] = (bool) $recipient->HideFormData; // Include any parsed merge field references from the CMS editor - this is already escaped + // This string substitution works for both HTML and plain text emails. + // $recipient->getEmailBodyContent() will retrieve the relevant version of the email $emailData['Body'] = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields); // Push the template data to the Email's data @@ -414,7 +416,8 @@ JS $this->extend('updateEmail', $email, $recipient, $emailData); if ((bool)$recipient->SendPlain) { - $body = strip_tags($recipient->getEmailBodyContent()) . "\n"; + // decode previously encoded html tags because the email is being sent as text/plain + $body = html_entity_decode($emailData['Body']) . "\n"; if (isset($emailData['Fields']) && !$emailData['HideFormData']) { foreach ($emailData['Fields'] as $field) { if ($field instanceof SubmittedFileField) { diff --git a/templates/SilverStripe/UserForms/Model/EditableFormField/EditableTextareaField.ss b/templates/SilverStripe/UserForms/Model/EditableFormField/EditableTextareaField.ss index dfe607c..790f73d 100644 --- a/templates/SilverStripe/UserForms/Model/EditableFormField/EditableTextareaField.ss +++ b/templates/SilverStripe/UserForms/Model/EditableFormField/EditableTextareaField.ss @@ -1 +1 @@ - + diff --git a/tests/php/Control/UserDefinedFormControllerTest.php b/tests/php/Control/UserDefinedFormControllerTest.php index 0c2e9a9..bc736ab 100644 --- a/tests/php/Control/UserDefinedFormControllerTest.php +++ b/tests/php/Control/UserDefinedFormControllerTest.php @@ -68,16 +68,18 @@ class UserDefinedFormControllerTest extends FunctionalTest // load the form $this->get($form->URLSegment); + /** @var EditableTextField $field */ $field = $this->objFromFixture(EditableTextField::class, 'basic-text'); - $response = $this->submitForm('UserForm_Form_' . $form->ID, null, [$field->Name => 'Basic Value']); + $data = [$field->Name => 'Basic Value HTML']; + $response = $this->submitForm('UserForm_Form_' . $form->ID, null, $data); // should have a submitted form field now $submitted = DataObject::get(SubmittedFormField::class, "\"Name\" = 'basic_text_name'"); $this->assertListAllMatch( [ 'Name' => 'basic_text_name', - 'Value' => 'Basic Value', + 'Value' => 'Basic Value HTML', 'Title' => 'Basic Text Field' ], $submitted @@ -93,8 +95,12 @@ class UserDefinedFormControllerTest extends FunctionalTest $this->assertEquals('Basic Text Field', (string) $title[0], 'Email contains the field name'); + // submitted html tags are escaped for the html value + $value = 'class="readonly">My body html Basic Value <b>HTML</b>'; + $this->assertTrue(strpos($email['Content'], $value) !== false, 'Email contains the merge field value'); + $value = $parser->getBySelector('dd'); - $this->assertEquals('Basic Value', (string) $value[0], 'Email contains the value'); + $this->assertEquals('Basic Value HTML', (string) $value[0], 'Email contains the value'); // no html $this->assertEmailSent('nohtml@example.com', 'no-reply@example.com', 'Email Subject'); @@ -102,6 +108,10 @@ class UserDefinedFormControllerTest extends FunctionalTest $this->assertContains('Basic Text Field: Basic Value', $nohtml['Content'], 'Email contains no html'); + // submitted html tags are not escaped because the email is being sent as text/plain + $value = 'My body text Basic Value HTML'; + $this->assertContains($value, $nohtml['Content'], 'Email contains the merge field value'); + // no data $this->assertEmailSent('nodata@example.com', 'no-reply@example.com', 'Email Subject'); $nodata = $this->findEmail('nodata@example.com', 'no-reply@example.com', 'Email Subject'); diff --git a/tests/php/Model/Recipient/EmailRecipientTest.php b/tests/php/Model/Recipient/EmailRecipientTest.php index 652a612..0de6ae8 100644 --- a/tests/php/Model/Recipient/EmailRecipientTest.php +++ b/tests/php/Model/Recipient/EmailRecipientTest.php @@ -5,6 +5,7 @@ namespace SilverStripe\UserForms\Tests\Model\Recipient; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Dev\SapphireTest; use SilverStripe\UserForms\Model\Recipient\EmailRecipient; +use SilverStripe\UserForms\Model\UserDefinedForm; class EmailRecipientTest extends SapphireTest { @@ -50,4 +51,19 @@ class EmailRecipientTest extends SapphireTest $this->assertSame('test2@example.com', $recipient->EmailFrom); $this->assertSame('test3@example.com', $recipient->EmailReplyTo); } + + public function testGetEmailTemplateDropdownValues() + { + $form = new UserDefinedForm(); + $form->write(); + $recipient = new EmailRecipient(); + $recipient->FormID = $form->ID; + $recipient->FormClass = UserDefinedForm::class; + $ds = DIRECTORY_SEPARATOR; + $expected = [ + "email{$ds}SubmittedFormEmail" => 'SubmittedFormEmail', + "email{$ds}SubmittedFormEmailPlain" => 'SubmittedFormEmailPlain' + ]; + $this->assertSame($expected, $recipient->getEmailTemplateDropdownValues()); + } } diff --git a/tests/php/UserFormsTest.yml b/tests/php/UserFormsTest.yml index 46cfd30..e48cc06 100644 --- a/tests/php/UserFormsTest.yml +++ b/tests/php/UserFormsTest.yml @@ -317,12 +317,14 @@ SilverStripe\UserForms\Model\Recipient\EmailRecipient: EmailAddress: test@example.com EmailSubject: Email Subject EmailFrom: no-reply@example.com + EmailBodyHtml: '
My body html $basic_text_name
' no-html: EmailAddress: nohtml@example.com EmailSubject: Email Subject EmailFrom: no-reply@example.com SendPlain: true + EmailBody: 'My body text $basic_text_name' no-data: EmailAddress: nodata@example.com