diff --git a/lib/EMailTemplate.php b/lib/EMailTemplate.php index 7461bbf..a4f9df1 100644 --- a/lib/EMailTemplate.php +++ b/lib/EMailTemplate.php @@ -7,12 +7,16 @@ use OC\Mail\EMailTemplate as ParentTemplate; use OCA\NcwMailtemplate\AppInfo\Application; use OCP\Defaults; +use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\IUser; +use OCP\IUserManager; use OCP\L10N\IFactory; class EMailTemplate extends ParentTemplate { private IL10N $l; + private ?IUser $user = null; // Generated asset URLs (filled in constructor) private string $spacerUrl = ''; @@ -54,17 +58,97 @@ public function __construct( ?int $logoHeight, string $emailId, array $data = [], - ) { + ) { + // Initialize parent first to set up basic properties parent::__construct($defaults, $urlGenerator, $l10nFactory, $logoWidth, $logoHeight, $emailId, $data); - - // Initialize localization object - $this->l = $l10nFactory->get(Application::APP_ID); + + // Try to get user from various sources (for recipient's language) + $this->user = $this->determineUser($this->data); + + // Get language: user's preference, or system default + if ($this->user) { + $lang = $this->l10nFactory->getUserLanguage($this->user); + } else { + $config = \OC::$server->get(IConfig::class); + $lang = $config->getSystemValue('default_language', 'en'); + } + $this->l = $this->l10nFactory->get(Application::APP_ID, $lang); // Generate URLs for template assets $this->generateTemplateAssetUrls($urlGenerator); - // Load all HTML template files + // Load all HTML template files - this will override parent's head and tail $this->loadHtmlTemplateFiles(); + + // Replace the parent's htmlBody that was set with the parent's head + // with our custom head + $this->htmlBody = $this->head; + } + + /** + * Determine the user for this email template + * Tries multiple sources: data array keys, current logged-in user, etc. + * + * Supported data keys (from various email types): + * - userid: from settings.Welcome + * - emailAddress: from settings.PasswordChanged, settings.EmailChanged + * - newEMailAddress: from settings.EmailChanged + * - shareWith: from file sharing emails (can be email or user ID) + * - displayname: from various emails + * - attendee_name: from calendar invitation emails + * + * @param array $data + * @return IUser|null + */ + private function determineUser(array $data): ?IUser { + $userManager = \OC::$server->get(IUserManager::class); + + // Priority 1: Try to get recipient by user ID (most direct) + $userIdKeys = ['userid', 'userId', 'uid']; + foreach ($userIdKeys as $key) { + if (isset($data[$key]) && is_string($data[$key])) { + $user = $userManager->get($data[$key]); + if ($user instanceof IUser) { + return $user; + } + } + } + + // Priority 2: Try to get recipient by email address + $emailKeys = ['emailAddress', 'newEMailAddress', 'shareWith']; + foreach ($emailKeys as $key) { + if (isset($data[$key]) && is_string($data[$key]) && str_contains($data[$key], '@')) { + $value = $data[$key]; + + // Try to get user by email address + $users = $userManager->getByEmail($value); + if (!empty($users)) { + return reset($users); + } + } + } + + // Priority 3: Try attendee_name or displayname as user ID or display name search + $nameKeys = ['attendee_name', 'displayname']; + foreach ($nameKeys as $key) { + if (isset($data[$key]) && is_string($data[$key])) { + $value = $data[$key]; + + // First try as user ID + $user = $userManager->get($value); + if ($user instanceof IUser) { + return $user; + } + + // Then try as display name search + $users = $userManager->searchDisplayName($value, 1); + if (!empty($users)) { + return reset($users); + } + } + } + + return null; } /** diff --git a/lib/templates/email/bodyEnd.html b/lib/templates/email/bodyEnd.html index 64ddfe7..4285827 100644 --- a/lib/templates/email/bodyEnd.html +++ b/lib/templates/email/bodyEnd.html @@ -1,6 +1,6 @@ diff --git a/lib/templates/email/bodyText.html b/lib/templates/email/bodyText.html index 4f01c61..a05ef52 100644 --- a/lib/templates/email/bodyText.html +++ b/lib/templates/email/bodyText.html @@ -1,6 +1,6 @@

- + %s

diff --git a/lib/templates/email/button.html b/lib/templates/email/button.html index 9d3a09b..5d1c5a2 100644 --- a/lib/templates/email/button.html +++ b/lib/templates/email/button.html @@ -2,7 +2,7 @@ diff --git a/lib/templates/email/buttonGroup.html b/lib/templates/email/buttonGroup.html index 6c675de..dfb4098 100644 --- a/lib/templates/email/buttonGroup.html +++ b/lib/templates/email/buttonGroup.html @@ -2,12 +2,12 @@
- + %7$s
diff --git a/lib/templates/email/footer.html b/lib/templates/email/footer.html index 4b46387..8b4247f 100644 --- a/lib/templates/email/footer.html +++ b/lib/templates/email/footer.html @@ -23,14 +23,14 @@ + +
- + %7$s - + %9$s

- t('Further information')?> + t('Further information')?>

diff --git a/lib/templates/email/head.html b/lib/templates/email/head.html index a6153c3..a1d8294 100644 --- a/lib/templates/email/head.html +++ b/lib/templates/email/head.html @@ -1,133 +1,15 @@ - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- - - -
- + + + + + + + + + + + + -
+
\ No newline at end of file diff --git a/lib/templates/email/header.html b/lib/templates/email/header.html index c6557a3..f77f530 100644 --- a/lib/templates/email/header.html +++ b/lib/templates/email/header.html @@ -21,8 +21,8 @@
 - IONOS Logo + + IONOS Logo  diff --git a/lib/templates/email/heading.html b/lib/templates/email/heading.html index 719844d..38a0164 100644 --- a/lib/templates/email/heading.html +++ b/lib/templates/email/heading.html @@ -19,7 +19,7 @@

- + %s

@@ -31,7 +31,7 @@
-  +
diff --git a/lib/templates/email/tail.html b/lib/templates/email/tail.html index b6792ab..e556a53 100644 --- a/lib/templates/email/tail.html +++ b/lib/templates/email/tail.html @@ -1,8 +1,15 @@ -
- - + + +
+ + + + + +
                                                           
+