From 4b3ad5f13c5d7cc1b5d1a6fd0f6a433544d17ff0 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 5 May 2023 10:40:01 +0200 Subject: [PATCH] feat(email-templates): Allow inserting HTML fragments in email templates Email templates currently only allow to add text bodies, lists and buttons. It's not possible to add formatting or other elements. This provides a way to inject arbitrary HTML in emails, which can be useful in some cases. Signed-off-by: Thomas Citharel --- lib/private/Mail/EMailTemplate.php | 19 +++++++++++++++++++ lib/public/Mail/IEMailTemplate.php | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/private/Mail/EMailTemplate.php b/lib/private/Mail/EMailTemplate.php index 40738623c192f..37d934817c798 100644 --- a/lib/private/Mail/EMailTemplate.php +++ b/lib/private/Mail/EMailTemplate.php @@ -597,6 +597,25 @@ public function addBodyButton(string $text, string $url, $plainText = '') { $this->plainBody .= $url . PHP_EOL; } + /** + * Adds a HTML fragment to the body of the email. Use only for basic HTML fragments and make sure the given HTML is valid, as this can affect the whole email HTML body + * + * @param string $html The HTML fragment to add to the body of the email + * @param string $plainText The plain text alternative version for the HTML fragment + * @since 27.0.0 + */ + public function addHTMLFragment(string $html, string $plainText): void { + if ($this->footerAdded) { + return; + } + + $this->ensureBodyListClosed(); + $this->ensureBodyIsOpened(); + + $this->htmlBody .= $html; + $this->plainBody .= $plainText . PHP_EOL . PHP_EOL; + } + /** * Close the HTML body when it is open */ diff --git a/lib/public/Mail/IEMailTemplate.php b/lib/public/Mail/IEMailTemplate.php index ea0fa970c9acd..4de9e62b59212 100644 --- a/lib/public/Mail/IEMailTemplate.php +++ b/lib/public/Mail/IEMailTemplate.php @@ -136,6 +136,15 @@ public function addBodyButtonGroup(string $textLeft, string $urlLeft, string $te */ public function addBodyButton(string $text, string $url, $plainText = ''); + /** + * Adds a HTML fragment to the body of the email. Use only for basic HTML fragments and make sure the given HTML is valid, as this can affect the whole email HTML body + * + * @param string $html The HTML fragment to add to the body of the email + * @param string $plainText The plain text alternative version for the HTML fragment + * @since 27.0.0 + */ + public function addHTMLFragment(string $html, string $plainText): void; + /** * Adds a logo and a text to the footer.
in the text will be replaced by new lines in the plain text email *