Merge pull request #1037 from creative-commoners/pulls/5.8/plain-text-merge-fields

FIX Include merge fields in plain text emails
This commit is contained in:
Maxime Rainville 2021-04-13 22:45:18 +12:00 committed by GitHub
commit 3993f019e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 4 deletions

View File

@ -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) {

View File

@ -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 <b>HTML</b>'];
$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 <b>HTML</b>',
'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 &lt;b&gt;HTML&lt;/b&gt;</span>';
$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 <b>HTML</b>', (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 <b>HTML</b>';
$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');

View File

@ -317,12 +317,14 @@ SilverStripe\UserForms\Model\Recipient\EmailRecipient:
EmailAddress: test@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
EmailBodyHtml: '<div class="form__field-holder"><span id="Form_ItemEditForm_MergeField" class="readonly">My body html $basic_text_name</span></div>'
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