From 5c5853bee0bd2c51e9663ca503fae938d02736cc Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Mon, 22 Jul 2024 22:43:31 +0200
Subject: [PATCH 01/23] [5.2] [WIP] Feat: Add mail template layout as wrapper
 for mails

---
 administrator/components/com_mails/config.xml |  34 ++++-
 .../components/com_mails/forms/template.xml   |  22 +++-
 .../src/Field/MailtemplateLayoutField.php     | 124 ++++++++++++++++++
 .../com_mails/src/Model/TemplateModel.php     |   2 +
 administrator/language/en-GB/com_config.ini   |   3 +
 administrator/language/en-GB/com_mails.ini    |   2 +
 layouts/joomla/mail/mailtemplate.php          | 113 ++++++++++++++++
 libraries/src/Mail/MailTemplate.php           |  61 ++++++++-
 8 files changed, 358 insertions(+), 3 deletions(-)
 create mode 100644 administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
 create mode 100644 layouts/joomla/mail/mailtemplate.php

diff --git a/administrator/components/com_mails/config.xml b/administrator/components/com_mails/config.xml
index 5b3b64a30a8b7..d834795ae95a5 100644
--- a/administrator/components/com_mails/config.xml
+++ b/administrator/components/com_mails/config.xml
@@ -4,7 +4,8 @@
 	<inlinehelp button="show"/>
 	<fieldset
 		name="mails_options"
-		label="COM_MAILS_CONFIG_MAIL_OPTIONS" >
+		label="COM_MAILS_CONFIG_MAIL_OPTIONS"
+		addfieldprefix="Joomla\Component\Mails\Administrator\Field" >
 
 		<field
 			name="mail_style"
@@ -49,6 +50,37 @@
 			exclude="administrator|api|cache|cli|components|includes|language|layouts|libraries|modules|plugins|templates|tmp"
 		/>
 
+		<field
+			name="disable_htmllayout"
+			type="radio"
+			label="COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_OFF_LABEL"
+			layout="joomla.form.field.radio.switcher"
+			default="1"
+			showon="mail_style:html[OR]mail_style:both"
+			>
+			<option value="0">JDISABLED</option>
+			<option value="1">JENABLED</option>
+		</field>
+
+		<field
+			name="mail_htmllayout"
+			type="mailtemplateLayout"
+			label="COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_LABEL"
+			class="form-select"
+			showon="mail_style:html[OR]mail_style:both[AND]disable_htmllayout:1"
+		/>
+
+		<field
+			name="mail_logoFile"
+			type="media"
+			schemes="http,https,ftp,ftps,data,file"
+			validate="url"
+			relative="true"
+			default=""
+			label="COM_CONFIG_FIELD_MAILTEMPLATE_LOGOFILE_LABEL"
+			showon="mail_style:html[OR]mail_style:both[AND]disable_htmllayout:1"
+		/>
+
 	</fieldset>
 
 	<fieldset
diff --git a/administrator/components/com_mails/forms/template.xml b/administrator/components/com_mails/forms/template.xml
index c7930835b435a..b11b279c21d4d 100644
--- a/administrator/components/com_mails/forms/template.xml
+++ b/administrator/components/com_mails/forms/template.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<form>
+<form addfieldprefix="Joomla\Component\Mails\Administrator\Field">
 	<fieldset>
 		<field
 			name="template_id"
@@ -205,6 +205,26 @@
 				default=""
 				filter="string"
 			/>
+
+			<field
+				name="disable_htmllayout"
+				type="radio"
+				label="COM_MAILS_FIELD_HTML_LAYOUT_OFF_LABEL"
+				layout="joomla.form.field.radio.switcher"
+				default="1"
+				>
+				<option value="0">JDISABLED</option>
+				<option value="1">JENABLED</option>
+			</field>
+
+			<field
+				name="htmllayout"
+				type="mailtemplateLayout"
+				label="COM_MAILS_FIELD_HTML_LAYOUT_LABEL"
+				class="form-select"
+				showon="disable_htmllayout:1"
+			/>
+
 		</fieldset>
 	</fields>
 </form>
diff --git a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
new file mode 100644
index 0000000000000..c844d070d8c13
--- /dev/null
+++ b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
@@ -0,0 +1,124 @@
+<?php
+
+/**
+ * @package     Joomla.Administrator
+ * @subpackage  com_mails
+ *
+ * @copyright   (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
+ * @license     GNU General Public License version 2 or later; see LICENSE.txt
+ */
+
+namespace Joomla\Component\Mails\Administrator\Field;
+
+use Joomla\CMS\Form\FormField;
+use Joomla\CMS\HTML\HTMLHelper;
+use Joomla\CMS\Language\Text;
+use Joomla\Filesystem\Folder;
+use Joomla\Filesystem\Path;
+
+// phpcs:disable PSR1.Files.SideEffects
+\defined('_JEXEC') or die;
+// phpcs:enable PSR1.Files.SideEffects
+
+/**
+ * Form Field to display a list of the layouts for a field from
+ * the extension or template overrides.
+ *
+ * @since  __DEPLOY_VERSION__
+ */
+class MailtemplateLayoutField extends FormField
+{
+    /**
+     * The form field type.
+     *
+     * @var    string
+     * @since  __DEPLOY_VERSION__
+     */
+    protected $type = 'MailtemplateLayout';
+
+    /**
+     * Method to get the field input for a field layout field.
+     *
+     * @return  string   The field input.
+     *
+     * @since   __DEPLOY_VERSION__
+     */
+    protected function getInput()
+    {
+        // $extension = explode('.', $this->form->getValue('context'));
+        // $extension = $extension[0];
+
+       // Get the database object and a new query object.
+        $db    = $this->getDatabase();
+        $query = $db->getQuery(true);
+
+        // Build the query.
+        $query->select('element, name')
+            ->from('#__extensions')
+            ->where('client_id = 0')
+            ->where('type = ' . $db->quote('template'))
+            ->where('enabled = 1');
+
+        // Set the query and load the templates.
+        $db->setQuery($query);
+        $templates = $db->loadObjectList('element');
+
+        // Prepare the grouped list
+        $groups = [];
+
+        // Add "Use Default"
+        $groups[]['items'][] = HTMLHelper::_('select.option', '', Text::_('JOPTION_USE_DEFAULT'));
+
+        // Loop on all templates
+        if ($templates) {
+            foreach ($templates as $template) {
+                $files          = [];
+                $template_paths = [
+                    Path::clean(JPATH_SITE . '/templates/' . $template->element . '/html/layouts/joomla/mail'),
+                    Path::clean(JPATH_SITE . '/templates/' . $template->element . '/html/layouts/com_mails/joomla/mail')
+                ];
+
+                // Add the layout options from the template paths.
+                foreach ($template_paths as $template_path) {
+                    if (is_dir($template_path)) {
+                        $files = array_merge($files, Folder::files($template_path, '^[^_]*\.php$', false, true));
+                    }
+                }
+
+                if (\count($files)) {
+                    // Create the group for the template
+                    $groups[$template->name]          = [];
+                    $groups[$template->name]['id']    = $this->id . '_' . $template->element;
+                    $groups[$template->name]['text']  = Text::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
+                    $groups[$template->name]['items'] = [];
+
+                    foreach ($files as $file) {
+                        // Add an option to the template group
+                        $value                              = basename($file, '.php');
+                        $groups[$template->name]['items'][] = HTMLHelper::_('select.option', $value, $value);
+                    }
+                }
+            }
+        }
+
+        // Compute attributes for the grouped list
+        $attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
+        $attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
+
+        // Prepare HTML code
+        $html = [];
+
+        // Compute the current selected values
+        $selected = [$this->value];
+
+        // Add a grouped list
+        $html[] = HTMLHelper::_(
+            'select.groupedlist',
+            $groups,
+            $this->name,
+            ['id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected]
+        );
+
+        return implode($html);
+    }
+}
diff --git a/administrator/components/com_mails/src/Model/TemplateModel.php b/administrator/components/com_mails/src/Model/TemplateModel.php
index 512edd3829f53..cc771e3854a7b 100644
--- a/administrator/components/com_mails/src/Model/TemplateModel.php
+++ b/administrator/components/com_mails/src/Model/TemplateModel.php
@@ -86,6 +86,8 @@ public function getForm($data = [], $loadData = true)
 
         if ($params->get('mail_style', 'plaintext') == 'plaintext') {
             $form->removeField('htmlbody');
+            $form->removeField('disable_htmllayout', 'params');
+            $form->removeField('htmllayout', 'params');
         }
 
         if ($params->get('mail_style', 'plaintext') == 'html') {
diff --git a/administrator/language/en-GB/com_config.ini b/administrator/language/en-GB/com_config.ini
index 5e3b1d1c1fc07..f49ed855b03d3 100644
--- a/administrator/language/en-GB/com_config.ini
+++ b/administrator/language/en-GB/com_config.ini
@@ -131,6 +131,9 @@ COM_CONFIG_FIELD_MAIL_SMTP_PASSWORD_LABEL="SMTP Password"
 COM_CONFIG_FIELD_MAIL_SMTP_PORT_LABEL="SMTP Port"
 COM_CONFIG_FIELD_MAIL_SMTP_SECURE_LABEL="SMTP Security"
 COM_CONFIG_FIELD_MAIL_SMTP_USERNAME_LABEL="SMTP Username"
+COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_LABEL="Mail Template Layout"
+COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_OFF_LABEL="Disable Mail Template Layout"
+COM_CONFIG_FIELD_MAILTEMPLATE_LOGOFILE_LABEL="Logo File"
 COM_CONFIG_FIELD_MEMCACHE_COMPRESSION_LABEL="Memcache(d) Compression"
 COM_CONFIG_FIELD_MEMCACHE_HOST_LABEL="Memcache(d) Server Host"
 COM_CONFIG_FIELD_MEMCACHE_PERSISTENT_LABEL="Persistent Memcache(d)"
diff --git a/administrator/language/en-GB/com_mails.ini b/administrator/language/en-GB/com_mails.ini
index 07c50249a56a4..e1a2bbaa593a1 100644
--- a/administrator/language/en-GB/com_mails.ini
+++ b/administrator/language/en-GB/com_mails.ini
@@ -18,6 +18,8 @@ COM_MAILS_FIELD_BODY_LABEL="Body"
 COM_MAILS_FIELD_FILE_LABEL="File"
 COM_MAILS_FIELD_FILENAME_LABEL="File Name"
 COM_MAILS_FIELD_HTMLBODY_LABEL="HTML Body"
+COM_MAILS_FIELD_HTML_LAYOUT_LABEL="Mail Template Layout"
+COM_MAILS_FIELD_HTML_LAYOUT_OFF_LABEL="Disable Mail Template Layout"
 COM_MAILS_FIELD_LANGUAGE_CODE_INVALID="Invalid Language Code"
 COM_MAILS_FIELD_MAIL_COPY_MAIL_LABEL="Send Copy To Email"
 COM_MAILS_FIELD_MAIL_FROM_EMAIL_LABEL="From Email"
diff --git a/layouts/joomla/mail/mailtemplate.php b/layouts/joomla/mail/mailtemplate.php
new file mode 100644
index 0000000000000..074a470a24065
--- /dev/null
+++ b/layouts/joomla/mail/mailtemplate.php
@@ -0,0 +1,113 @@
+<?php
+
+/**
+ * @package     Joomla.Site
+ * @subpackage  com_mails
+ *
+ * @copyright   (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
+ * @license     GNU General Public License version 2 or later; see LICENSE.txt
+ */
+
+use Joomla\CMS\Uri\Uri;
+
+defined('_JEXEC') or die;
+
+// Check if we have all the data
+if (!array_key_exists('mail', $displayData)) {
+    return;
+}
+
+// Setting up for display
+$mailBody = $displayData['mail'];
+
+if (!$mailBody) {
+    return;
+}
+
+$extraData = [];
+
+if (array_key_exists('extra', $displayData)) {
+    $extraData = $displayData['extra'];
+}
+
+$siteUrl = Uri::root(false);
+
+?>
+<!DOCTYPE html>
+<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
+    <head>
+        <meta charset="utf-8">
+        <meta name="viewport" content="width=device-width,initial-scale=1">
+        <meta name="x-apple-disable-message-reformatting">
+        <!--[if !mso]><!-->
+        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+        <!--<![endif]-->
+        <title></title>
+        <!--[if mso]>
+            <style>
+                table {border-collapse:collapse;border-spacing:0;border:none;margin:0;}
+                div, td {padding:0;}
+                div {margin:0 !important;}
+                </style>
+            <noscript>
+                <xml>
+                <o:OfficeDocumentSettings>
+                    <o:PixelsPerInch>96</o:PixelsPerInch>
+                </o:OfficeDocumentSettings>
+                </xml>
+            </noscript>
+            <![endif]-->
+        <style>
+            html {height: 100%;}
+            table, td, div, h1, p { font-family: Arial, sans-serif; }
+        </style>
+    </head>
+    <body style="margin:0;padding:0;word-spacing:normal;background-color:#00000008;height:100%;">
+        <div role="article" aria-roledescription="email" lang="en" style="text-size-adjust:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;background-color:#00000008;height:100%;">
+            <table role="presentation" style="width:100%;border:none;border-spacing:0;height:100%;">
+                <tr>
+                    <td align="center" style="vertical-align:baseline; padding:30px 0">
+                        <!--[if mso]>
+                        <table role="presentation" align="center" style="width:630px;">
+                        <tr>
+                        <td>
+                        <![endif]-->
+                        <table role="presentation" style="width:94%;max-width:630px;border:none;border-spacing:0;text-align:left;font-family:Arial,sans-serif;font-size:16px;line-height:22px;color:#363636;">
+                            <tr>
+                                <td style="padding:40px 30px 0 30px;text-align:center;font-size:24px;font-weight:bold;background-color:#ffffff;">
+                                <?php if (isset($extraData['logo']) || isset($extraData['siteName'])) : ?>
+                                    <?php if (isset($extraData['logo'])) : ?>
+                                    <img src="cid:<?php echo htmlspecialchars($extraData['logo'], ENT_QUOTES);?>" alt="<?php echo (isset($extraData['siteName']) ? $extraData['siteName'] : 'Logo');?>" style="max-width:80%;height:auto;border:none;text-decoration:none;color:#ffffff;">
+                                    <?php else: ?>
+                                    <h1 style="margin-top:0;margin-bottom:0;font-size:26px;line-height:32px;font-weight:bold;letter-spacing:-0.02em;color:#112855;">
+                                        <?php echo $extraData['siteName']; ?>
+                                    </h1>
+                                    <?php endif; ?>
+                                    <div style="padding: 30px 0 0;"></div>
+                                    <div style="padding:.75px;background-color:#0000000f;"></div>
+                                </td>
+                            </tr>
+                            <tr>
+                                <td style="padding:30px;background-color:#ffffff;">
+                                <?php endif; ?>
+                                    <?php echo $mailBody; ?>
+                                </td>
+                            </tr>
+                            <tr>
+                                <td style="padding:30px;text-align:center;font-size:12px;background-color:#112855;color:#cccccc;">
+                                    <p style="margin:0;font-size:14px;line-height:20px;">&reg; <?php echo isset($extraData['siteName']) ? $extraData['siteName'] . ' ' : ''; ?><?php echo date("Y"); ?>
+                                    <br><a title="<?php echo $siteUrl;?>" href="<?php echo $siteUrl; ?>" style="color:#cccccc;text-decoration:underline;"><?php echo substr_replace($siteUrl, 'www.',0,strlen(Uri::getInstance()->toString(['scheme'])));?></a>
+                                </td>
+                            </tr>
+                        </table>
+                        <!--[if mso]>
+                        </td>
+                        </tr>
+                        </table>
+                        <![endif]-->
+                    </td>
+                </tr>
+            </table>
+        </div>
+    </body>
+</html>
diff --git a/libraries/src/Mail/MailTemplate.php b/libraries/src/Mail/MailTemplate.php
index 710db97b95c50..65292d3183062 100644
--- a/libraries/src/Mail/MailTemplate.php
+++ b/libraries/src/Mail/MailTemplate.php
@@ -11,8 +11,12 @@
 
 use Joomla\CMS\Component\ComponentHelper;
 use Joomla\CMS\Factory;
+use Joomla\CMS\HTML\HTMLHelper;
 use Joomla\CMS\Language\Text;
+use Joomla\CMS\Layout\FileLayout;
+use Joomla\CMS\Layout\LayoutHelper;
 use Joomla\CMS\Mail\Exception\MailDisabledException;
+use Joomla\Component\Templates\Administrator\Helper\TemplatesHelper;
 use Joomla\Database\ParameterType;
 use Joomla\Filesystem\File;
 use Joomla\Filesystem\Path;
@@ -90,6 +94,14 @@ class MailTemplate
      */
     protected $replyto;
 
+    /**
+     * Layout mailtemplate options of the email
+     *
+     * @var    string[]
+     * @since  __DEPLOY_VERSION__
+     */
+    protected $layoutTemplateData = [];
+
     /**
      * Constructor for the mail templating class
      *
@@ -167,6 +179,20 @@ public function setReplyTo($mail, $name = '')
         $this->replyto = $reply;
     }
 
+    /**
+     * Add data to the html layout template
+     *
+     * @param   array  $data  Associative array of strings for the layout
+     *
+     * @return  void
+     *
+     * @since   __DEPLOY_VERSION__
+     */
+    public function addLayoutTemplateData($data)
+    {
+        $this->layoutTemplateData = array_merge($this->layoutTemplateData, $data);
+    }
+
     /**
      * Add data to replace in the template
      *
@@ -245,10 +271,11 @@ public function send()
         $this->mailer->setSubject($subject);
 
         $mailStyle = $config->get('mail_style', 'plaintext');
+        $useLayout = $params->get('disable_htmllayout', $config->get('disable_htmllayout', '1'));
         // Use the plain-text replacement data, if specified.
         $plainData = $this->plain_data ?: $this->data;
         $plainBody = $this->replaceTags(Text::_($mail->body), $plainData);
-        $htmlBody  = $this->replaceTags(Text::_($mail->htmlbody), $this->data);
+        $htmlBody  = $useLayout ? Text::_($mail->htmlbody) : $this->replaceTags(Text::_($mail->htmlbody), $this->data);
 
         if ($mailStyle === 'plaintext' || $mailStyle === 'both') {
             // If the Plain template is empty try to convert the HTML template to a Plain text
@@ -274,6 +301,38 @@ public function send()
 
             $htmlBody = MailHelper::convertRelativeToAbsoluteUrls($htmlBody);
 
+            if ($useLayout) {
+                // Add additional data to the layout template
+                $this->addLayoutTemplateData(['siteName' => $app->get('sitename')]);
+
+                // Add the logo to the mail as inline attachement
+                if ($logo = (string) $config->get('mail_logoFile', '')) {
+                    $logo = Path::clean(JPATH_ROOT . '/' . htmlspecialchars(HTMLHelper::cleanImageURL($logo)->url));
+                    if (is_file($logo)) {
+                        # Attach the logo as inline attachement
+                        $this->mailer->addAttachment($logo, basename($logo), 'base64', mime_content_type($logo), 'inline');
+
+                        // We need only the cid for attached logo file
+                        $this->addLayoutTemplateData(['logo' => basename($logo)]);
+                    }
+                }
+
+                $layout = $params->get('htmllayout', $config->get('mail_htmllayout', 'mailtemplate'));
+                // Wrap the default Joomla mail template around the HTML body
+                $layoutFile   = new FileLayout('joomla.mail.' . $layout, null, ['client' => 'site']);
+                // Set the default template search path
+                foreach (TemplatesHelper::getTemplateOptions(0) as $siteTemplate) {
+                    $layoutFile->addIncludePaths([
+                        JPATH_SITE . '/templates/' . $siteTemplate->value . '/html/layouts',
+                        JPATH_SITE . '/templates/' . $siteTemplate->value . '/html/layouts/com_mails',
+                    ]);
+                }
+                
+                $htmlBody = $layoutFile->render(['mail' => $htmlBody, 'extra' => $this->layoutTemplateData], null);
+
+                $htmlBody  = $this->replaceTags(Text::_($htmlBody), $this->data);
+            }
+
             $this->mailer->setBody($htmlBody);
         }
 

From 67ca7f923cff29829843f414612b3760a75768ca Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Tue, 23 Jul 2024 11:33:37 +0200
Subject: [PATCH 02/23] cs

---
 layouts/joomla/mail/mailtemplate.php | 4 ++--
 libraries/src/Mail/MailTemplate.php  | 3 +--
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/layouts/joomla/mail/mailtemplate.php b/layouts/joomla/mail/mailtemplate.php
index 074a470a24065..ff9090e9e0af5 100644
--- a/layouts/joomla/mail/mailtemplate.php
+++ b/layouts/joomla/mail/mailtemplate.php
@@ -78,7 +78,7 @@
                                 <?php if (isset($extraData['logo']) || isset($extraData['siteName'])) : ?>
                                     <?php if (isset($extraData['logo'])) : ?>
                                     <img src="cid:<?php echo htmlspecialchars($extraData['logo'], ENT_QUOTES);?>" alt="<?php echo (isset($extraData['siteName']) ? $extraData['siteName'] : 'Logo');?>" style="max-width:80%;height:auto;border:none;text-decoration:none;color:#ffffff;">
-                                    <?php else: ?>
+                                    <?php else : ?>
                                     <h1 style="margin-top:0;margin-bottom:0;font-size:26px;line-height:32px;font-weight:bold;letter-spacing:-0.02em;color:#112855;">
                                         <?php echo $extraData['siteName']; ?>
                                     </h1>
@@ -96,7 +96,7 @@
                             <tr>
                                 <td style="padding:30px;text-align:center;font-size:12px;background-color:#112855;color:#cccccc;">
                                     <p style="margin:0;font-size:14px;line-height:20px;">&reg; <?php echo isset($extraData['siteName']) ? $extraData['siteName'] . ' ' : ''; ?><?php echo date("Y"); ?>
-                                    <br><a title="<?php echo $siteUrl;?>" href="<?php echo $siteUrl; ?>" style="color:#cccccc;text-decoration:underline;"><?php echo substr_replace($siteUrl, 'www.',0,strlen(Uri::getInstance()->toString(['scheme'])));?></a>
+                                    <br><a title="<?php echo $siteUrl;?>" href="<?php echo $siteUrl; ?>" style="color:#cccccc;text-decoration:underline;"><?php echo substr_replace($siteUrl, 'www.', 0, strlen(Uri::getInstance()->toString(['scheme'])));?></a>
                                 </td>
                             </tr>
                         </table>
diff --git a/libraries/src/Mail/MailTemplate.php b/libraries/src/Mail/MailTemplate.php
index 65292d3183062..7d1803b01a318 100644
--- a/libraries/src/Mail/MailTemplate.php
+++ b/libraries/src/Mail/MailTemplate.php
@@ -14,7 +14,6 @@
 use Joomla\CMS\HTML\HTMLHelper;
 use Joomla\CMS\Language\Text;
 use Joomla\CMS\Layout\FileLayout;
-use Joomla\CMS\Layout\LayoutHelper;
 use Joomla\CMS\Mail\Exception\MailDisabledException;
 use Joomla\Component\Templates\Administrator\Helper\TemplatesHelper;
 use Joomla\Database\ParameterType;
@@ -327,7 +326,7 @@ public function send()
                         JPATH_SITE . '/templates/' . $siteTemplate->value . '/html/layouts/com_mails',
                     ]);
                 }
-                
+
                 $htmlBody = $layoutFile->render(['mail' => $htmlBody, 'extra' => $this->layoutTemplateData], null);
 
                 $htmlBody  = $this->replaceTags(Text::_($htmlBody), $this->data);

From 110f5c8bb2e21dda0a0bac27865586c8c2758cc2 Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Tue, 23 Jul 2024 12:16:11 +0200
Subject: [PATCH 03/23] fix cs

---
 .../components/com_mails/src/Field/MailtemplateLayoutField.php  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
index c844d070d8c13..d0fd27cb42a26 100644
--- a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
+++ b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
@@ -75,7 +75,7 @@ protected function getInput()
                 $files          = [];
                 $template_paths = [
                     Path::clean(JPATH_SITE . '/templates/' . $template->element . '/html/layouts/joomla/mail'),
-                    Path::clean(JPATH_SITE . '/templates/' . $template->element . '/html/layouts/com_mails/joomla/mail')
+                    Path::clean(JPATH_SITE . '/templates/' . $template->element . '/html/layouts/com_mails/joomla/mail'),
                 ];
 
                 // Add the layout options from the template paths.

From 65284e5b072739995da22038e06cb8a32d6b6928 Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Tue, 23 Jul 2024 12:27:13 +0200
Subject: [PATCH 04/23] cs

---
 .../components/com_mails/src/Field/MailtemplateLayoutField.php  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
index d0fd27cb42a26..0bd7c7a59c6c9 100644
--- a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
+++ b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
@@ -48,7 +48,7 @@ protected function getInput()
         // $extension = explode('.', $this->form->getValue('context'));
         // $extension = $extension[0];
 
-       // Get the database object and a new query object.
+        // Get the database object and a new query object.
         $db    = $this->getDatabase();
         $query = $db->getQuery(true);
 

From c7a9bf0da4f876d5e7246a0556dbf758d1e6f1ec Mon Sep 17 00:00:00 2001
From: Martina  Scholz <64533137+LadySolveig@users.noreply.github.com>
Date: Tue, 23 Jul 2024 12:30:06 +0200
Subject: [PATCH 05/23] Fix order in xml field

Co-authored-by: Brian Teeman <brian@teeman.net>
---
 administrator/components/com_mails/config.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/administrator/components/com_mails/config.xml b/administrator/components/com_mails/config.xml
index d834795ae95a5..427516f7529a6 100644
--- a/administrator/components/com_mails/config.xml
+++ b/administrator/components/com_mails/config.xml
@@ -73,11 +73,11 @@
 		<field
 			name="mail_logoFile"
 			type="media"
+			label="COM_CONFIG_FIELD_MAILTEMPLATE_LOGOFILE_LABEL"
 			schemes="http,https,ftp,ftps,data,file"
 			validate="url"
 			relative="true"
 			default=""
-			label="COM_CONFIG_FIELD_MAILTEMPLATE_LOGOFILE_LABEL"
 			showon="mail_style:html[OR]mail_style:both[AND]disable_htmllayout:1"
 		/>
 

From 641d6aaed9b2d1dfd7756851bf25d0dca1724c7d Mon Sep 17 00:00:00 2001
From: Martina  Scholz <64533137+LadySolveig@users.noreply.github.com>
Date: Tue, 23 Jul 2024 12:50:37 +0200
Subject: [PATCH 06/23] Update layouts/joomla/mail/mailtemplate.php

Co-authored-by: Brian Teeman <brian@teeman.net>
---
 layouts/joomla/mail/mailtemplate.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/layouts/joomla/mail/mailtemplate.php b/layouts/joomla/mail/mailtemplate.php
index ff9090e9e0af5..127fb87d24043 100644
--- a/layouts/joomla/mail/mailtemplate.php
+++ b/layouts/joomla/mail/mailtemplate.php
@@ -95,7 +95,7 @@
                             </tr>
                             <tr>
                                 <td style="padding:30px;text-align:center;font-size:12px;background-color:#112855;color:#cccccc;">
-                                    <p style="margin:0;font-size:14px;line-height:20px;">&reg; <?php echo isset($extraData['siteName']) ? $extraData['siteName'] . ' ' : ''; ?><?php echo date("Y"); ?>
+                                    <p style="margin:0;font-size:14px;line-height:20px;">&copy; <?php echo isset($extraData['siteName']) ? $extraData['siteName'] . ' ' : ''; ?><?php echo date("Y"); ?>
                                     <br><a title="<?php echo $siteUrl;?>" href="<?php echo $siteUrl; ?>" style="color:#cccccc;text-decoration:underline;"><?php echo substr_replace($siteUrl, 'www.', 0, strlen(Uri::getInstance()->toString(['scheme'])));?></a>
                                 </td>
                             </tr>

From cd3e5afaf11bfbe802e65d05cbf25399d8a68812 Mon Sep 17 00:00:00 2001
From: Martina  Scholz <64533137+LadySolveig@users.noreply.github.com>
Date: Tue, 23 Jul 2024 12:58:49 +0200
Subject: [PATCH 07/23] Update administrator/language/en-GB/com_config.ini

Co-authored-by: Brian Teeman <brian@teeman.net>
---
 administrator/language/en-GB/com_config.ini | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/administrator/language/en-GB/com_config.ini b/administrator/language/en-GB/com_config.ini
index f49ed855b03d3..3f85d0a5e78d7 100644
--- a/administrator/language/en-GB/com_config.ini
+++ b/administrator/language/en-GB/com_config.ini
@@ -131,7 +131,7 @@ COM_CONFIG_FIELD_MAIL_SMTP_PASSWORD_LABEL="SMTP Password"
 COM_CONFIG_FIELD_MAIL_SMTP_PORT_LABEL="SMTP Port"
 COM_CONFIG_FIELD_MAIL_SMTP_SECURE_LABEL="SMTP Security"
 COM_CONFIG_FIELD_MAIL_SMTP_USERNAME_LABEL="SMTP Username"
-COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_LABEL="Mail Template Layout"
+COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_LABEL="Layout"
 COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_OFF_LABEL="Disable Mail Template Layout"
 COM_CONFIG_FIELD_MAILTEMPLATE_LOGOFILE_LABEL="Logo File"
 COM_CONFIG_FIELD_MEMCACHE_COMPRESSION_LABEL="Memcache(d) Compression"

From 16d272e0e120e7dc2aa883d88f11bf10e16615a8 Mon Sep 17 00:00:00 2001
From: Martina  Scholz <64533137+LadySolveig@users.noreply.github.com>
Date: Tue, 23 Jul 2024 12:59:07 +0200
Subject: [PATCH 08/23] Update administrator/language/en-GB/com_config.ini

Co-authored-by: Brian Teeman <brian@teeman.net>
---
 administrator/language/en-GB/com_config.ini | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/administrator/language/en-GB/com_config.ini b/administrator/language/en-GB/com_config.ini
index 3f85d0a5e78d7..d896ccdc803d1 100644
--- a/administrator/language/en-GB/com_config.ini
+++ b/administrator/language/en-GB/com_config.ini
@@ -132,7 +132,7 @@ COM_CONFIG_FIELD_MAIL_SMTP_PORT_LABEL="SMTP Port"
 COM_CONFIG_FIELD_MAIL_SMTP_SECURE_LABEL="SMTP Security"
 COM_CONFIG_FIELD_MAIL_SMTP_USERNAME_LABEL="SMTP Username"
 COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_LABEL="Layout"
-COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_OFF_LABEL="Disable Mail Template Layout"
+COM_CONFIG_FIELD_MAILTEMPLATE_LAYOUT_OFF_LABEL="Mail Template Layout"
 COM_CONFIG_FIELD_MAILTEMPLATE_LOGOFILE_LABEL="Logo File"
 COM_CONFIG_FIELD_MEMCACHE_COMPRESSION_LABEL="Memcache(d) Compression"
 COM_CONFIG_FIELD_MEMCACHE_HOST_LABEL="Memcache(d) Server Host"

From 9697528e8f10fad52a7d53496e382fc99f3faec8 Mon Sep 17 00:00:00 2001
From: Martina  Scholz <64533137+LadySolveig@users.noreply.github.com>
Date: Tue, 23 Jul 2024 12:59:50 +0200
Subject: [PATCH 09/23] Update administrator/language/en-GB/com_mails.ini

Co-authored-by: Brian Teeman <brian@teeman.net>
---
 administrator/language/en-GB/com_mails.ini | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/administrator/language/en-GB/com_mails.ini b/administrator/language/en-GB/com_mails.ini
index e1a2bbaa593a1..d594b3adaedf7 100644
--- a/administrator/language/en-GB/com_mails.ini
+++ b/administrator/language/en-GB/com_mails.ini
@@ -18,7 +18,7 @@ COM_MAILS_FIELD_BODY_LABEL="Body"
 COM_MAILS_FIELD_FILE_LABEL="File"
 COM_MAILS_FIELD_FILENAME_LABEL="File Name"
 COM_MAILS_FIELD_HTMLBODY_LABEL="HTML Body"
-COM_MAILS_FIELD_HTML_LAYOUT_LABEL="Mail Template Layout"
+COM_MAILS_FIELD_HTML_LAYOUT_LABEL="Layout"
 COM_MAILS_FIELD_HTML_LAYOUT_OFF_LABEL="Disable Mail Template Layout"
 COM_MAILS_FIELD_LANGUAGE_CODE_INVALID="Invalid Language Code"
 COM_MAILS_FIELD_MAIL_COPY_MAIL_LABEL="Send Copy To Email"

From e0dbe300c4c53eda5808414242a4f573b88995fb Mon Sep 17 00:00:00 2001
From: Martina  Scholz <64533137+LadySolveig@users.noreply.github.com>
Date: Tue, 23 Jul 2024 13:00:20 +0200
Subject: [PATCH 10/23] Update administrator/language/en-GB/com_mails.ini

Co-authored-by: Brian Teeman <brian@teeman.net>
---
 administrator/language/en-GB/com_mails.ini | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/administrator/language/en-GB/com_mails.ini b/administrator/language/en-GB/com_mails.ini
index d594b3adaedf7..ad2a8acb55023 100644
--- a/administrator/language/en-GB/com_mails.ini
+++ b/administrator/language/en-GB/com_mails.ini
@@ -19,7 +19,7 @@ COM_MAILS_FIELD_FILE_LABEL="File"
 COM_MAILS_FIELD_FILENAME_LABEL="File Name"
 COM_MAILS_FIELD_HTMLBODY_LABEL="HTML Body"
 COM_MAILS_FIELD_HTML_LAYOUT_LABEL="Layout"
-COM_MAILS_FIELD_HTML_LAYOUT_OFF_LABEL="Disable Mail Template Layout"
+COM_MAILS_FIELD_HTML_LAYOUT_OFF_LABEL="Mail Template Layout"
 COM_MAILS_FIELD_LANGUAGE_CODE_INVALID="Invalid Language Code"
 COM_MAILS_FIELD_MAIL_COPY_MAIL_LABEL="Send Copy To Email"
 COM_MAILS_FIELD_MAIL_FROM_EMAIL_LABEL="From Email"

From cc4d8fc5836727a9e4149bf26935f77b327ec9c2 Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Tue, 23 Jul 2024 14:58:22 +0200
Subject: [PATCH 11/23] Remove modification in layout that was replacing the
 scheme with  'www.' from the site URL link text

---
 layouts/joomla/mail/mailtemplate.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/layouts/joomla/mail/mailtemplate.php b/layouts/joomla/mail/mailtemplate.php
index 127fb87d24043..108a154f18e6f 100644
--- a/layouts/joomla/mail/mailtemplate.php
+++ b/layouts/joomla/mail/mailtemplate.php
@@ -96,7 +96,7 @@
                             <tr>
                                 <td style="padding:30px;text-align:center;font-size:12px;background-color:#112855;color:#cccccc;">
                                     <p style="margin:0;font-size:14px;line-height:20px;">&copy; <?php echo isset($extraData['siteName']) ? $extraData['siteName'] . ' ' : ''; ?><?php echo date("Y"); ?>
-                                    <br><a title="<?php echo $siteUrl;?>" href="<?php echo $siteUrl; ?>" style="color:#cccccc;text-decoration:underline;"><?php echo substr_replace($siteUrl, 'www.', 0, strlen(Uri::getInstance()->toString(['scheme'])));?></a>
+                                    <br><a title="<?php echo $siteUrl;?>" href="<?php echo $siteUrl; ?>" style="color:#cccccc;text-decoration:underline;"><?php echo $siteUrl; ?></a>
                                 </td>
                             </tr>
                         </table>

From 1f53da6ce2c36f270f1c5d2b24fa33c96abe7a81 Mon Sep 17 00:00:00 2001
From: Martina  Scholz <64533137+LadySolveig@users.noreply.github.com>
Date: Tue, 23 Jul 2024 15:04:22 +0200
Subject: [PATCH 12/23] Update layouts/joomla/mail/mailtemplate.php

Co-authored-by: Brian Teeman <brian@teeman.net>
---
 layouts/joomla/mail/mailtemplate.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/layouts/joomla/mail/mailtemplate.php b/layouts/joomla/mail/mailtemplate.php
index 108a154f18e6f..d649fbb811f9c 100644
--- a/layouts/joomla/mail/mailtemplate.php
+++ b/layouts/joomla/mail/mailtemplate.php
@@ -2,7 +2,7 @@
 
 /**
  * @package     Joomla.Site
- * @subpackage  com_mails
+ * @subpackage  Layout
  *
  * @copyright   (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
  * @license     GNU General Public License version 2 or later; see LICENSE.txt

From 6ebad04b9310ceb37beb2fe11956c5eb8950e84b Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Thu, 25 Jul 2024 09:48:07 +0000
Subject: [PATCH 13/23] various improvements, fixes and additional option
 `disable logofile`

- Add additional option to disable attachement of logofile to mail and useglobal in template.xml
- point to the right template of the selected layout override
- only show additional options per Mail Template if option is diabled in Global Configuration
---
 .../components/com_mails/forms/template.xml   | 24 ++++++++++---
 .../src/Field/MailtemplateLayoutField.php     | 17 ++++++---
 .../com_mails/src/Model/TemplateModel.php     |  4 +++
 administrator/language/en-GB/com_mails.ini    |  1 +
 libraries/src/Mail/MailTemplate.php           | 36 +++++++++++++++----
 5 files changed, 66 insertions(+), 16 deletions(-)

diff --git a/administrator/components/com_mails/forms/template.xml b/administrator/components/com_mails/forms/template.xml
index b11b279c21d4d..b327c536a656b 100644
--- a/administrator/components/com_mails/forms/template.xml
+++ b/administrator/components/com_mails/forms/template.xml
@@ -208,10 +208,11 @@
 
 			<field
 				name="disable_htmllayout"
-				type="radio"
+				type="list"
 				label="COM_MAILS_FIELD_HTML_LAYOUT_OFF_LABEL"
-				layout="joomla.form.field.radio.switcher"
-				default="1"
+				validate="options"
+				class="form-select-color-state"
+				useglobal="true"
 				>
 				<option value="0">JDISABLED</option>
 				<option value="1">JENABLED</option>
@@ -222,9 +223,24 @@
 				type="mailtemplateLayout"
 				label="COM_MAILS_FIELD_HTML_LAYOUT_LABEL"
 				class="form-select"
-				showon="disable_htmllayout:1"
+				useglobal="true"
+				default=""
+				showon="disable_htmllayout!:0"
 			/>
 
+			<field
+				name="disable_logofile"
+				type="radio"
+				label="COM_MAILS_FIELD_HTML_LAYOUT_LOGO_OFF_LABEL"
+				layout="joomla.form.field.radio.switcher"
+				default="1"
+				showon="disable_htmllayout!:0[AND]htmllayout!:mailtemplate"
+				validate="options"
+				>
+				<option value="0">JDISABLED</option>
+				<option value="1">JENABLED</option>
+			</field>
+
 		</fieldset>
 	</fields>
 </form>
diff --git a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
index 0bd7c7a59c6c9..79d6811dd68fb 100644
--- a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
+++ b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
@@ -10,6 +10,7 @@
 
 namespace Joomla\Component\Mails\Administrator\Field;
 
+use Joomla\CMS\Factory;
 use Joomla\CMS\Form\FormField;
 use Joomla\CMS\HTML\HTMLHelper;
 use Joomla\CMS\Language\Text;
@@ -45,8 +46,7 @@ class MailtemplateLayoutField extends FormField
      */
     protected function getInput()
     {
-        // $extension = explode('.', $this->form->getValue('context'));
-        // $extension = $extension[0];
+        $lang = Factory::getApplication()->getLanguage();
 
         // Get the database object and a new query object.
         $db    = $this->getDatabase();
@@ -67,7 +67,12 @@ protected function getInput()
         $groups = [];
 
         // Add "Use Default"
-        $groups[]['items'][] = HTMLHelper::_('select.option', '', Text::_('JOPTION_USE_DEFAULT'));
+        $groups[]['items'][] = HTMLHelper::_('select.option', 'mailtemplate', Text::_('JOPTION_USE_DEFAULT'));
+
+        // Add a Use Global option if useglobal="true" in XML file
+        if ((string) $this->element['useglobal'] === 'true') {
+            $groups[Text::_('JOPTION_FROM_STANDARD')]['items'][] = HTMLHelper::_('select.option', '', Text::_('JGLOBAL_USE_GLOBAL'));
+        }
 
         // Loop on all templates
         if ($templates) {
@@ -94,8 +99,10 @@ protected function getInput()
 
                     foreach ($files as $file) {
                         // Add an option to the template group
-                        $value                              = basename($file, '.php');
-                        $groups[$template->name]['items'][] = HTMLHelper::_('select.option', $value, $value);
+                        $value = basename($file, '.php');
+                        $text  = $lang->hasKey($key = strtoupper('TPL_' . $template->element . '_MAILTEMPLATE_LAYOUT_' . $value))
+                                    ? Text::_($key) : $value;
+                        $groups[$template->name]['items'][] = HTMLHelper::_('select.option', $template->element . ':' . $value, $text);
                     }
                 }
             }
diff --git a/administrator/components/com_mails/src/Model/TemplateModel.php b/administrator/components/com_mails/src/Model/TemplateModel.php
index cc771e3854a7b..c6849b31a4de3 100644
--- a/administrator/components/com_mails/src/Model/TemplateModel.php
+++ b/administrator/components/com_mails/src/Model/TemplateModel.php
@@ -88,6 +88,7 @@ public function getForm($data = [], $loadData = true)
             $form->removeField('htmlbody');
             $form->removeField('disable_htmllayout', 'params');
             $form->removeField('htmllayout', 'params');
+            $form->removeField('disable_logofile', 'params');
         }
 
         if ($params->get('mail_style', 'plaintext') == 'html') {
@@ -108,6 +109,9 @@ public function getForm($data = [], $loadData = true)
             $form->removeField('smtpauth', 'params');
             $form->removeField('smtpuser', 'params');
             $form->removeField('smtppass', 'params');
+            $form->removeField('disable_htmllayout', 'params');
+            $form->removeField('htmllayout', 'params');
+            $form->removeField('disable_logofile', 'params');
         }
 
         if (!$params->get('copy_mails')) {
diff --git a/administrator/language/en-GB/com_mails.ini b/administrator/language/en-GB/com_mails.ini
index ad2a8acb55023..01388cfdffc7b 100644
--- a/administrator/language/en-GB/com_mails.ini
+++ b/administrator/language/en-GB/com_mails.ini
@@ -20,6 +20,7 @@ COM_MAILS_FIELD_FILENAME_LABEL="File Name"
 COM_MAILS_FIELD_HTMLBODY_LABEL="HTML Body"
 COM_MAILS_FIELD_HTML_LAYOUT_LABEL="Layout"
 COM_MAILS_FIELD_HTML_LAYOUT_OFF_LABEL="Mail Template Layout"
+COM_MAILS_FIELD_HTML_LAYOUT_LOGO_OFF_LABEL="Logo File"
 COM_MAILS_FIELD_LANGUAGE_CODE_INVALID="Invalid Language Code"
 COM_MAILS_FIELD_MAIL_COPY_MAIL_LABEL="Send Copy To Email"
 COM_MAILS_FIELD_MAIL_FROM_EMAIL_LABEL="From Email"
diff --git a/libraries/src/Mail/MailTemplate.php b/libraries/src/Mail/MailTemplate.php
index 7d1803b01a318..76906dacadc48 100644
--- a/libraries/src/Mail/MailTemplate.php
+++ b/libraries/src/Mail/MailTemplate.php
@@ -264,13 +264,19 @@ public function send()
             $replyToName = $params->get('replytoname', $replyToName);
         }
 
+        $useLayout   = $config->get('disable_htmllayout', '1');
+
+        if ((int) $config->get('alternative_mailconfig', 0) === 1) {
+            $useLayout   = $params->get('disable_htmllayout', $useLayout);
+        }
+
         $app->triggerEvent('onMailBeforeRendering', [$this->template_id, &$this]);
 
         $subject = $this->replaceTags(Text::_($mail->subject), $this->data);
         $this->mailer->setSubject($subject);
 
         $mailStyle = $config->get('mail_style', 'plaintext');
-        $useLayout = $params->get('disable_htmllayout', $config->get('disable_htmllayout', '1'));
+
         // Use the plain-text replacement data, if specified.
         $plainData = $this->plain_data ?: $this->data;
         $plainBody = $this->replaceTags(Text::_($mail->body), $plainData);
@@ -304,8 +310,17 @@ public function send()
                 // Add additional data to the layout template
                 $this->addLayoutTemplateData(['siteName' => $app->get('sitename')]);
 
+                $layout = $config->get('mail_htmllayout', 'mailtemplate');
+                $logo   = (string) $config->get('mail_logoFile', '');
+
+                // Check alternative mailconfig
+                if ((int) $config->get('alternative_mailconfig', 0) === 1) {
+                    $layout = $params->get('htmllayout', $layout);
+                    $logo = $params->get('disable_logofile', 1) ? $logo : '' ;
+                }
+
                 // Add the logo to the mail as inline attachement
-                if ($logo = (string) $config->get('mail_logoFile', '')) {
+                if ($logo) {
                     $logo = Path::clean(JPATH_ROOT . '/' . htmlspecialchars(HTMLHelper::cleanImageURL($logo)->url));
                     if (is_file($logo)) {
                         # Attach the logo as inline attachement
@@ -316,14 +331,21 @@ public function send()
                     }
                 }
 
-                $layout = $params->get('htmllayout', $config->get('mail_htmllayout', 'mailtemplate'));
+
+                // Check if layout is a template override
+                $layoutParts = explode(':', $layout);
+
+                if (count($layoutParts) === 2) {
+                    $layout = $layoutParts[1];
+                }
+
                 // Wrap the default Joomla mail template around the HTML body
                 $layoutFile   = new FileLayout('joomla.mail.' . $layout, null, ['client' => 'site']);
-                // Set the default template search path
-                foreach (TemplatesHelper::getTemplateOptions(0) as $siteTemplate) {
+                // Set the template layout path if needed
+                if (count($layoutParts) === 2) {
                     $layoutFile->addIncludePaths([
-                        JPATH_SITE . '/templates/' . $siteTemplate->value . '/html/layouts',
-                        JPATH_SITE . '/templates/' . $siteTemplate->value . '/html/layouts/com_mails',
+                        JPATH_SITE . '/templates/' . $layoutParts[0] . '/html/layouts',
+                        JPATH_SITE . '/templates/' . $layoutParts[0] . '/html/layouts/com_mails',
                     ]);
                 }
 

From 277f842884ba39b08a004535a6b13f78aba9d2b6 Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Thu, 25 Jul 2024 12:05:59 +0200
Subject: [PATCH 14/23] phpcs-fixer + suggestions from review

---
 administrator/components/com_mails/config.xml |  2 +-
 libraries/src/Mail/MailTemplate.php           | 13 ++++++-------
 2 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/administrator/components/com_mails/config.xml b/administrator/components/com_mails/config.xml
index 427516f7529a6..20ce6becdf990 100644
--- a/administrator/components/com_mails/config.xml
+++ b/administrator/components/com_mails/config.xml
@@ -5,7 +5,7 @@
 	<fieldset
 		name="mails_options"
 		label="COM_MAILS_CONFIG_MAIL_OPTIONS"
-		addfieldprefix="Joomla\Component\Mails\Administrator\Field" >
+		addfieldprefix="Joomla\Component\Mails\Administrator\Field">
 
 		<field
 			name="mail_style"
diff --git a/libraries/src/Mail/MailTemplate.php b/libraries/src/Mail/MailTemplate.php
index 76906dacadc48..4b07c76f00f59 100644
--- a/libraries/src/Mail/MailTemplate.php
+++ b/libraries/src/Mail/MailTemplate.php
@@ -15,7 +15,6 @@
 use Joomla\CMS\Language\Text;
 use Joomla\CMS\Layout\FileLayout;
 use Joomla\CMS\Mail\Exception\MailDisabledException;
-use Joomla\Component\Templates\Administrator\Helper\TemplatesHelper;
 use Joomla\Database\ParameterType;
 use Joomla\Filesystem\File;
 use Joomla\Filesystem\Path;
@@ -316,7 +315,7 @@ public function send()
                 // Check alternative mailconfig
                 if ((int) $config->get('alternative_mailconfig', 0) === 1) {
                     $layout = $params->get('htmllayout', $layout);
-                    $logo = $params->get('disable_logofile', 1) ? $logo : '' ;
+                    $logo   = $params->get('disable_logofile', 1) ? $logo : '' ;
                 }
 
                 // Add the logo to the mail as inline attachement
@@ -331,18 +330,18 @@ public function send()
                     }
                 }
 
-
                 // Check if layout is a template override
                 $layoutParts = explode(':', $layout);
 
-                if (count($layoutParts) === 2) {
+                if (\count($layoutParts) === 2) {
                     $layout = $layoutParts[1];
                 }
 
                 // Wrap the default Joomla mail template around the HTML body
-                $layoutFile   = new FileLayout('joomla.mail.' . $layout, null, ['client' => 'site']);
+                $layoutFile = new FileLayout('joomla.mail.' . $layout, null, ['client' => 'site']);
+
                 // Set the template layout path if needed
-                if (count($layoutParts) === 2) {
+                if (\count($layoutParts) === 2) {
                     $layoutFile->addIncludePaths([
                         JPATH_SITE . '/templates/' . $layoutParts[0] . '/html/layouts',
                         JPATH_SITE . '/templates/' . $layoutParts[0] . '/html/layouts/com_mails',
@@ -351,7 +350,7 @@ public function send()
 
                 $htmlBody = $layoutFile->render(['mail' => $htmlBody, 'extra' => $this->layoutTemplateData], null);
 
-                $htmlBody  = $this->replaceTags(Text::_($htmlBody), $this->data);
+                $htmlBody = $this->replaceTags(Text::_($htmlBody), $this->data);
             }
 
             $this->mailer->setBody($htmlBody);

From f3ff8950fe705d7b61484d2a8f41cea091e1280f Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Thu, 25 Jul 2024 12:24:04 +0200
Subject: [PATCH 15/23] ISO 2 Letter Language Code dynamic in layout

---
 layouts/joomla/mail/mailtemplate.php | 2 +-
 libraries/src/Mail/MailTemplate.php  | 5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/layouts/joomla/mail/mailtemplate.php b/layouts/joomla/mail/mailtemplate.php
index 108a154f18e6f..21d0b584e31f5 100644
--- a/layouts/joomla/mail/mailtemplate.php
+++ b/layouts/joomla/mail/mailtemplate.php
@@ -34,7 +34,7 @@
 
 ?>
 <!DOCTYPE html>
-<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
+<html lang="<?php echo (isset($extraData['lang'])) ?  $extraData['lang'] : 'en' ?>" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
     <head>
         <meta charset="utf-8">
         <meta name="viewport" content="width=device-width,initial-scale=1">
diff --git a/libraries/src/Mail/MailTemplate.php b/libraries/src/Mail/MailTemplate.php
index 4b07c76f00f59..e7f448ae9fe9f 100644
--- a/libraries/src/Mail/MailTemplate.php
+++ b/libraries/src/Mail/MailTemplate.php
@@ -307,7 +307,10 @@ public function send()
 
             if ($useLayout) {
                 // Add additional data to the layout template
-                $this->addLayoutTemplateData(['siteName' => $app->get('sitename')]);
+                $this->addLayoutTemplateData([
+                    'siteName' => $app->get('sitename'),
+                    'lang'     => \substr($this->language, 0, 2),
+                ]);
 
                 $layout = $config->get('mail_htmllayout', 'mailtemplate');
                 $logo   = (string) $config->get('mail_logoFile', '');

From 69fec91121846cab9f47b187c55e6ec58d91ac81 Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Thu, 25 Jul 2024 13:08:08 +0200
Subject: [PATCH 16/23] Fixed cid 'site-logo' for attached logo image

---
 layouts/joomla/mail/mailtemplate.php | 2 +-
 libraries/src/Mail/MailTemplate.php  | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/layouts/joomla/mail/mailtemplate.php b/layouts/joomla/mail/mailtemplate.php
index 21d0b584e31f5..034331a0ef660 100644
--- a/layouts/joomla/mail/mailtemplate.php
+++ b/layouts/joomla/mail/mailtemplate.php
@@ -77,7 +77,7 @@
                                 <td style="padding:40px 30px 0 30px;text-align:center;font-size:24px;font-weight:bold;background-color:#ffffff;">
                                 <?php if (isset($extraData['logo']) || isset($extraData['siteName'])) : ?>
                                     <?php if (isset($extraData['logo'])) : ?>
-                                    <img src="cid:<?php echo htmlspecialchars($extraData['logo'], ENT_QUOTES);?>" alt="<?php echo (isset($extraData['siteName']) ? $extraData['siteName'] : 'Logo');?>" style="max-width:80%;height:auto;border:none;text-decoration:none;color:#ffffff;">
+                                    <img src="cid:<?php echo htmlspecialchars($extraData['logo'], ENT_QUOTES);?>" alt="<?php echo (isset($extraData['siteName']) ? $extraData['siteName'] . ' ' : '');?>Logo" style="max-width:80%;height:auto;border:none;text-decoration:none;color:#ffffff;">
                                     <?php else : ?>
                                     <h1 style="margin-top:0;margin-bottom:0;font-size:26px;line-height:32px;font-weight:bold;letter-spacing:-0.02em;color:#112855;">
                                         <?php echo $extraData['siteName']; ?>
diff --git a/libraries/src/Mail/MailTemplate.php b/libraries/src/Mail/MailTemplate.php
index e7f448ae9fe9f..df78c17180ec8 100644
--- a/libraries/src/Mail/MailTemplate.php
+++ b/libraries/src/Mail/MailTemplate.php
@@ -326,10 +326,10 @@ public function send()
                     $logo = Path::clean(JPATH_ROOT . '/' . htmlspecialchars(HTMLHelper::cleanImageURL($logo)->url));
                     if (is_file($logo)) {
                         # Attach the logo as inline attachement
-                        $this->mailer->addAttachment($logo, basename($logo), 'base64', mime_content_type($logo), 'inline');
+                        $this->mailer->addAttachment($logo, 'site-logo', 'base64', mime_content_type($logo), 'inline');
 
                         // We need only the cid for attached logo file
-                        $this->addLayoutTemplateData(['logo' => basename($logo)]);
+                        $this->addLayoutTemplateData(['logo' => 'site-logo']);
                     }
                 }
 

From 2f5ed0d1bd7d100e2fe7963c44ef2b5b8e7c6ab9 Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Thu, 25 Jul 2024 13:34:57 +0200
Subject: [PATCH 17/23] Allow image files with whitespace and rename variable
 name

---
 administrator/components/com_mails/config.xml | 2 +-
 libraries/src/Mail/MailTemplate.php           | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/administrator/components/com_mails/config.xml b/administrator/components/com_mails/config.xml
index 20ce6becdf990..1f72f4994b347 100644
--- a/administrator/components/com_mails/config.xml
+++ b/administrator/components/com_mails/config.xml
@@ -71,7 +71,7 @@
 		/>
 
 		<field
-			name="mail_logoFile"
+			name="mail_logofile"
 			type="media"
 			label="COM_CONFIG_FIELD_MAILTEMPLATE_LOGOFILE_LABEL"
 			schemes="http,https,ftp,ftps,data,file"
diff --git a/libraries/src/Mail/MailTemplate.php b/libraries/src/Mail/MailTemplate.php
index df78c17180ec8..8c9705dd5d305 100644
--- a/libraries/src/Mail/MailTemplate.php
+++ b/libraries/src/Mail/MailTemplate.php
@@ -313,7 +313,7 @@ public function send()
                 ]);
 
                 $layout = $config->get('mail_htmllayout', 'mailtemplate');
-                $logo   = (string) $config->get('mail_logoFile', '');
+                $logo   = (string) $config->get('mail_logofile', '');
 
                 // Check alternative mailconfig
                 if ((int) $config->get('alternative_mailconfig', 0) === 1) {
@@ -323,8 +323,8 @@ public function send()
 
                 // Add the logo to the mail as inline attachement
                 if ($logo) {
-                    $logo = Path::clean(JPATH_ROOT . '/' . htmlspecialchars(HTMLHelper::cleanImageURL($logo)->url));
-                    if (is_file($logo)) {
+                    $logo = Path::check(JPATH_ROOT . '/' . HTMLHelper::cleanImageURL($logo)->url);
+                    if (is_file(\urldecode($logo))) {
                         # Attach the logo as inline attachement
                         $this->mailer->addAttachment($logo, 'site-logo', 'base64', mime_content_type($logo), 'inline');
 

From 44d956e4b2cf1780e0548e634b9bf460955745cb Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Thu, 25 Jul 2024 13:56:50 +0200
Subject: [PATCH 18/23] dynamic lang code part 2

---
 layouts/joomla/mail/mailtemplate.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/layouts/joomla/mail/mailtemplate.php b/layouts/joomla/mail/mailtemplate.php
index a3a09b351008a..b369fb0da2402 100644
--- a/layouts/joomla/mail/mailtemplate.php
+++ b/layouts/joomla/mail/mailtemplate.php
@@ -63,7 +63,7 @@
         </style>
     </head>
     <body style="margin:0;padding:0;word-spacing:normal;background-color:#00000008;height:100%;">
-        <div role="article" aria-roledescription="email" lang="en" style="text-size-adjust:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;background-color:#00000008;height:100%;">
+        <div role="article" aria-roledescription="email" lang="<?php echo (isset($extraData['lang'])) ?  $extraData['lang'] : 'en' ?>" style="text-size-adjust:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;background-color:#00000008;height:100%;">
             <table role="presentation" style="width:100%;border:none;border-spacing:0;height:100%;">
                 <tr>
                     <td align="center" style="vertical-align:baseline; padding:30px 0">

From 83724a03bc3a0e60218d30f812bf94f6e343d975 Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Thu, 25 Jul 2024 15:55:36 +0200
Subject: [PATCH 19/23] cs and remove second lang definition from layout

---
 layouts/joomla/mail/mailtemplate.php | 2 +-
 libraries/src/Mail/MailTemplate.php  | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/layouts/joomla/mail/mailtemplate.php b/layouts/joomla/mail/mailtemplate.php
index b369fb0da2402..209b0ad26f570 100644
--- a/layouts/joomla/mail/mailtemplate.php
+++ b/layouts/joomla/mail/mailtemplate.php
@@ -63,7 +63,7 @@
         </style>
     </head>
     <body style="margin:0;padding:0;word-spacing:normal;background-color:#00000008;height:100%;">
-        <div role="article" aria-roledescription="email" lang="<?php echo (isset($extraData['lang'])) ?  $extraData['lang'] : 'en' ?>" style="text-size-adjust:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;background-color:#00000008;height:100%;">
+        <div role="article" aria-roledescription="email" style="text-size-adjust:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;background-color:#00000008;height:100%;">
             <table role="presentation" style="width:100%;border:none;border-spacing:0;height:100%;">
                 <tr>
                     <td align="center" style="vertical-align:baseline; padding:30px 0">
diff --git a/libraries/src/Mail/MailTemplate.php b/libraries/src/Mail/MailTemplate.php
index 8c9705dd5d305..8f4147a3c637f 100644
--- a/libraries/src/Mail/MailTemplate.php
+++ b/libraries/src/Mail/MailTemplate.php
@@ -309,7 +309,7 @@ public function send()
                 // Add additional data to the layout template
                 $this->addLayoutTemplateData([
                     'siteName' => $app->get('sitename'),
-                    'lang'     => \substr($this->language, 0, 2),
+                    'lang'     => substr($this->language, 0, 2),
                 ]);
 
                 $layout = $config->get('mail_htmllayout', 'mailtemplate');
@@ -324,7 +324,7 @@ public function send()
                 // Add the logo to the mail as inline attachement
                 if ($logo) {
                     $logo = Path::check(JPATH_ROOT . '/' . HTMLHelper::cleanImageURL($logo)->url);
-                    if (is_file(\urldecode($logo))) {
+                    if (is_file(urldecode($logo))) {
                         # Attach the logo as inline attachement
                         $this->mailer->addAttachment($logo, 'site-logo', 'base64', mime_content_type($logo), 'inline');
 

From 1e2cb33de6bd104cd7b813f848b2e67323d48b7d Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Thu, 25 Jul 2024 16:28:53 +0200
Subject: [PATCH 20/23] Fix no tatic HtmlHelper

---
 libraries/src/Mail/MailTemplate.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libraries/src/Mail/MailTemplate.php b/libraries/src/Mail/MailTemplate.php
index 8f4147a3c637f..411dc01881cb5 100644
--- a/libraries/src/Mail/MailTemplate.php
+++ b/libraries/src/Mail/MailTemplate.php
@@ -323,7 +323,7 @@ public function send()
 
                 // Add the logo to the mail as inline attachement
                 if ($logo) {
-                    $logo = Path::check(JPATH_ROOT . '/' . HTMLHelper::cleanImageURL($logo)->url);
+                    $logo = Path::check(JPATH_ROOT . '/' . HTMLHelper::_('cleanImageURL', $logo)->url);
                     if (is_file(urldecode($logo))) {
                         # Attach the logo as inline attachement
                         $this->mailer->addAttachment($logo, 'site-logo', 'base64', mime_content_type($logo), 'inline');

From 334c6d9003942270932a835f1158567704c9dc65 Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Fri, 26 Jul 2024 10:56:10 +0200
Subject: [PATCH 21/23] Fix db->quote missing

---
 .../com_mails/src/Field/MailtemplateLayoutField.php         | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
index 79d6811dd68fb..0c05cf5ae6bd7 100644
--- a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
+++ b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
@@ -55,9 +55,9 @@ protected function getInput()
         // Build the query.
         $query->select('element, name')
             ->from('#__extensions')
-            ->where('client_id = 0')
-            ->where('type = ' . $db->quote('template'))
-            ->where('enabled = 1');
+            ->where($db->quoteName('client_id') . ' = ' . $db->quote('0'))
+            ->where($db->quoteName('type') . '=' . $db->quote('template'))
+            ->where($db->quoteName('enabled') . ' = ' . $db->quote('1'));
 
         // Set the query and load the templates.
         $db->setQuery($query);

From c2499df091fb16043a53001638df4d42c1de7dec Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Fri, 26 Jul 2024 11:31:25 +0200
Subject: [PATCH 22/23] remove empty space

---
 .../com_mails/src/Field/MailtemplateLayoutField.php           | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
index 0c05cf5ae6bd7..990b25aac3e13 100644
--- a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
+++ b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
@@ -55,9 +55,9 @@ protected function getInput()
         // Build the query.
         $query->select('element, name')
             ->from('#__extensions')
-            ->where($db->quoteName('client_id') . ' = ' . $db->quote('0'))
+            ->where($db->quoteName('client_id') . '=' . $db->quote('0'))
             ->where($db->quoteName('type') . '=' . $db->quote('template'))
-            ->where($db->quoteName('enabled') . ' = ' . $db->quote('1'));
+            ->where($db->quoteName('enabled') . '=' . $db->quote('1'));
 
         // Set the query and load the templates.
         $db->setQuery($query);

From ca816aea6cabe2dcf858175b49c0a8496a4749ed Mon Sep 17 00:00:00 2001
From: Martina Scholz <martina@simplysmart-it.de>
Date: Fri, 26 Jul 2024 17:08:45 +0200
Subject: [PATCH 23/23] improve query

---
 .../com_mails/src/Field/MailtemplateLayoutField.php         | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
index 990b25aac3e13..3ed4897764cb0 100644
--- a/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
+++ b/administrator/components/com_mails/src/Field/MailtemplateLayoutField.php
@@ -55,9 +55,9 @@ protected function getInput()
         // Build the query.
         $query->select('element, name')
             ->from('#__extensions')
-            ->where($db->quoteName('client_id') . '=' . $db->quote('0'))
-            ->where($db->quoteName('type') . '=' . $db->quote('template'))
-            ->where($db->quoteName('enabled') . '=' . $db->quote('1'));
+            ->where($db->quoteName('client_id') . ' = 0')
+            ->where($db->quoteName('type') . ' = ' . $db->quote('template'))
+            ->where($db->quoteName('enabled') . ' = 1');
 
         // Set the query and load the templates.
         $db->setQuery($query);