From 93a05dc777ec438c1dbdbba9d99a6f2ac2d38ffa Mon Sep 17 00:00:00 2001 From: Cameron Date: Sat, 10 Dec 2022 08:47:01 -0800 Subject: [PATCH] Closes #4919 - Enhancement: plugins may now use their own custom email templates with sendEmail(); --- e107_handlers/mail.php | 24 +++++-- .../_blank/templates/_blank_template.php | 15 ++++- e107_tests/tests/unit/e107EmailTest.php | 64 ++++++++++++++++++- 3 files changed, 91 insertions(+), 12 deletions(-) diff --git a/e107_handlers/mail.php b/e107_handlers/mail.php index 42067d520..9df0ffc54 100644 --- a/e107_handlers/mail.php +++ b/e107_handlers/mail.php @@ -912,13 +912,25 @@ class e107Email extends PHPMailer if(!empty($eml['template'])) // @see e107_core/templates/email_template.php { require_once(e_LANGUAGEDIR.e_LANGUAGE."/admin/lan_users.php"); // do not use e107::lan etc. - if($tmpl = e107::getCoreTemplate('email', $eml['template'], 'front', true)) //FIXME - Core template is failing with template 'notify'. Works with theme template. Issue with core template registry? + + if(is_array($eml['template']) && !empty($eml['template']['plugin']) && !empty($eml['template']['name']) && !empty($eml['template']['key'])) + { + $tmpl = e107::getTemplate($eml['template']['plugin'],$eml['template']['name'], $eml['template']['key']); + } + else + { + $tmpl = e107::getCoreTemplate('email', $eml['template'], 'front', true); + } + + if(!empty($tmpl)) //FIXME - Core template is failing with template 'notify'. Works with theme template. Issue with core template registry? { $eml['templateHTML'] = $tmpl; $eml['shortcodes'] = $this->processShortcodes($eml); - $eml['shortcodes']['_WRAPPER_'] = 'email/'.$eml['template']; - - $emailBody = $tmpl['header']. str_replace('{BODY}', $eml['body'], $tmpl['body']) . $tmpl['footer']; + if(is_string($eml['template'])) + { + $eml['shortcodes']['_WRAPPER_'] = 'email/'.$eml['template']; + } + $emailBody = varset($tmpl['header']). str_replace('{BODY}', $eml['body'], $tmpl['body']) . varset($tmpl['footer']); $eml['body'] = $tp->parseTemplate($emailBody, true, $eml['shortcodes']); @@ -935,7 +947,7 @@ class e107Email extends PHPMailer unset($eml['add_html_header']); // disable other headers when template is used. - $this->Subject = $tp->parseTemplate($tmpl['subject'], true, varset($eml['shortcodes'],null)); + $this->Subject = $tp->parseTemplate(varset($tmpl['subject'],'{SUBJECT}'), true, varset($eml['shortcodes'],null)); if($this->debug) { @@ -946,7 +958,7 @@ class e107Email extends PHPMailer { if($this->debug) { - echo "

Couldn't find email template: ".$eml['template']."

"; + echo "

Couldn't find email template: ".print_r($eml['template'],true)."

"; } // $emailBody = $eml['body']; diff --git a/e107_plugins/_blank/templates/_blank_template.php b/e107_plugins/_blank/templates/_blank_template.php index 92a40c2b7..6f81ee26b 100644 --- a/e107_plugins/_blank/templates/_blank_template.php +++ b/e107_plugins/_blank/templates/_blank_template.php @@ -9,8 +9,17 @@ */ - $_BLANK_WRAPPER['default']['BLANK_TEST'] = "[ {---} ]"; - $_BLANK_TEMPLATE['default'] = "
{BLANK_TEST}
"; +$_BLANK_WRAPPER['default']['BLANK_TEST'] = "[ {---} ]"; +$_BLANK_TEMPLATE['default'] = "
{BLANK_TEST}
"; - $_BLANK_TEMPLATE['other'] = "
{BLANK_TEST}
"; \ No newline at end of file +$_BLANK_TEMPLATE['other'] = "
{BLANK_TEST}
"; +$_BLANK_TEMPLATE['other'] = "
{BLANK_TEST}
"; + +/** + * Custom Plugin email template + * @see https://github.com/e107inc/e107/issues/4919 + */ +$_BLANK_TEMPLATE['email']['header'] = ''; +$_BLANK_TEMPLATE['email']['body'] = "
{NAME} {DATE}
{BODY}
"; +$_BLANK_TEMPLATE['email']['footer'] = ''; \ No newline at end of file diff --git a/e107_tests/tests/unit/e107EmailTest.php b/e107_tests/tests/unit/e107EmailTest.php index 65c4ad945..58bacd680 100644 --- a/e107_tests/tests/unit/e107EmailTest.php +++ b/e107_tests/tests/unit/e107EmailTest.php @@ -24,7 +24,7 @@ } catch(Exception $e) { - $this->assertTrue(false, "Couldn't load e107Email object"); + $this->fail("Couldn't load e107Email object"); } @@ -68,6 +68,64 @@ $this->assertStringNotContainsString('{MEDIA1}', $this->eml->Body); } + /** + * Test using an email template from e107_plugins/_blank/templates/_blank_template.php + * @return void + */ + public function testArraySetPluginTemplate() + { + $eml = array( + 'subject' => "[PLUGIN TEMPLATE EXAMPLE]", + 'sender_email' => "noreply@test.com", + 'sender_name' => "Test Person", + 'replyto' => "", + 'html' => true, + 'priority' => 1, + 'template' => ['plugin'=>'_blank', 'name'=>'_blank', 'key'=>'email'], + 'body' => "This is the body text", + 'cc' => '', + 'shortcodes' => [ + 'NAME' => "TestName", + 'DATE' => 'Jan 1st, 2020' + ], + ); + + $this->eml->arraySet($eml); + + $this->assertStringContainsString("noreply@test.com", $this->eml->From); + $this->assertStringContainsString("Test Person", $this->eml->FromName); + $this->assertStringContainsString("[PLUGIN TEMPLATE EXAMPLE]", $this->eml->Subject); + + $this->assertStringContainsString('', $this->eml->Body); + + $this->assertStringContainsString('
TestName Jan 1st, 2020
This is the body text
', $this->eml->Body); + $this->assertStringNotContainsString('{MEDIA1}', $this->eml->Body); + } + + + public function testArraySetNotifyTemplate() + { + $eml = array( + 'subject' => "[URGENT EXAMPLE]", + 'sender_email' => "noreply@test.com", + 'sender_name' => "Test Person", + 'replyto' => "", + 'html' => true, + 'priority' => 1, + 'template' => 'notify', + 'body' => "This is the body text", + 'cc' => '' + ); + + $this->eml->arraySet($eml); + + $this->assertStringContainsString("noreply@test.com", $this->eml->From); + $this->assertStringContainsString("Test Person", $this->eml->FromName); + $this->assertStringContainsString("e107: [URGENT EXAMPLE] ", $this->eml->Subject); + $this->assertStringContainsString("This is the body text", $this->eml->Body); + $this->assertStringContainsString("
", $this->eml->Body); + $this->assertStringNotContainsString('{MEDIA1}', $this->eml->Body); + } /* public function testMakePrintableAddress() { @@ -89,7 +147,7 @@ $html = "\n Hi Joe
-Check out http://e107.org
+Check out https://e107.org

Thanks,
Admin
@@ -106,7 +164,7 @@ Admin
$this->eml->MsgHTML($html); $result = json_encode($this->eml->AltBody); - $expected = '"Hi Joe\\nCheck out http:\\/\\/e107.org\\n\\nThanks,\\nAdmin\\n\\nWebsite:\\thttps:\\/\\/e107.org\\t\\nGithub:\\thttps:\\/\\/github.com\\/e107inc\\/"'; + $expected = '"Hi Joe\\nCheck out https:\\/\\/e107.org\\n\\nThanks,\\nAdmin\\n\\nWebsite:\\thttps:\\/\\/e107.org\\t\\nGithub:\\thttps:\\/\\/github.com\\/e107inc\\/"'; $this->assertSame($expected, $result); }