From 28ec50b18fd9f78a5f8965209be1a4940a96667b Mon Sep 17 00:00:00 2001 From: Christian Beeznest Date: Sat, 20 Sep 2025 06:46:13 -0500 Subject: [PATCH 1/2] Message: Fix email FROM builder; drop invalid setting fallback --- src/CoreBundle/Helpers/MessageHelper.php | 36 ++++++++++++++++-------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/CoreBundle/Helpers/MessageHelper.php b/src/CoreBundle/Helpers/MessageHelper.php index b960a62d8e6..da813a47149 100644 --- a/src/CoreBundle/Helpers/MessageHelper.php +++ b/src/CoreBundle/Helpers/MessageHelper.php @@ -299,24 +299,30 @@ private function addSenderAsReceiver(Message $message, User $sender): void } } + /** + * Sends an email notification to $receiver with given subject/content and optional attachments. + * - Validates recipient email. + * - Uses buildFromAddress() to construct a proper FROM (name + address). + * - Attaches only OK-uploaded files. + */ private function sendEmailNotification(User $receiver, User $sender, string $subject, string $content, array $attachmentList): void { + // Validate recipient email early $toAddress = $receiver->getEmail(); if (!filter_var($toAddress, FILTER_VALIDATE_EMAIL)) { + // No valid recipient → nothing to send (could log if needed) return; } - $from = $this->buildFromAddress(); - try { $email = (new Email()) - ->from($from) - ->to(new Address($toAddress, $receiver->getFullName() ?: $receiver->getUsername())) + ->from($this->buildFromAddress()) + ->to(new Address($toAddress, $receiver->getFullname() ?: $receiver->getUsername())) ->subject($subject) ->text($content) - ->html($content) - ; + ->html($content); + // Attach files if provided in the expected structure foreach ($attachmentList as $att) { $file = $att['file'] ?? null; if ($file instanceof UploadedFile && UPLOAD_ERR_OK === $file->getError()) { @@ -325,24 +331,28 @@ private function sendEmailNotification(User $receiver, User $sender, string $sub } $this->mailer->send($email); - } catch (Throwable $e) { - error_log('Failed to send email: '.$e->getMessage()); + } catch (\Throwable $e) { + // Soft-fail: log and continue + error_log('Failed to send email: ' . $e->getMessage()); } } /** - * Constructs the FROM as an Address, prioritizing configuration; - * If there is no valid value, infers the current domain and uses noreply@{domain}. + * Builds the FROM address used in outgoing emails. + * Priority (name): mail.mailer_from_name → platform.site_name → "Chamilo" + * Priority (email): mail.mailer_from_email → platform.administrator_email → noreply@{host} + * Host resolution: AccessUrl → RequestStack → 'example.org' */ private function buildFromAddress(): Address { + // Resolve display name $fromName = $this->settingsManager->getSetting('mail.mailer_from_name') ?: $this->settingsManager->getSetting('platform.site_name', true) ?: 'Chamilo'; + // Resolve email candidates (only existing/valid settings) $candidates = [ $this->settingsManager->getSetting('mail.mailer_from_email'), - $this->settingsManager->getSetting('mail.mailer_from_address'), $this->settingsManager->getSetting('platform.administrator_email'), ]; foreach ($candidates as $cand) { @@ -351,6 +361,7 @@ private function buildFromAddress(): Address } } + // Fallback host inference $host = null; $accessUrl = $this->accessUrlHelper->getCurrent(); if ($accessUrl && method_exists($accessUrl, 'getUrl')) { @@ -365,7 +376,8 @@ private function buildFromAddress(): Address $host = 'example.org'; } - return new Address('noreply@'.$host, $fromName); + // Last-resort fallback + return new Address('noreply@' . $host, $fromName); } /** From 37344433d5c91148a2e00edfb358a0f4a095d448 Mon Sep 17 00:00:00 2001 From: Christian Beeznest Date: Sat, 20 Sep 2025 06:49:46 -0500 Subject: [PATCH 2/2] Message: Minor typo in user name accessor --- src/CoreBundle/Helpers/MessageHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CoreBundle/Helpers/MessageHelper.php b/src/CoreBundle/Helpers/MessageHelper.php index da813a47149..26a1258922b 100644 --- a/src/CoreBundle/Helpers/MessageHelper.php +++ b/src/CoreBundle/Helpers/MessageHelper.php @@ -317,7 +317,7 @@ private function sendEmailNotification(User $receiver, User $sender, string $sub try { $email = (new Email()) ->from($this->buildFromAddress()) - ->to(new Address($toAddress, $receiver->getFullname() ?: $receiver->getUsername())) + ->to(new Address($toAddress, $receiver->getFullName() ?: $receiver->getUsername())) ->subject($subject) ->text($content) ->html($content);