Skip to content

Commit cff5ea9

Browse files
Merge pull request #6781 from christianbeeznest/fixes-updates136
Message: Fix email FROM builder; drop invalid setting fallback
2 parents c5e5401 + 3734443 commit cff5ea9

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/CoreBundle/Helpers/MessageHelper.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,30 @@ private function addSenderAsReceiver(Message $message, User $sender): void
299299
}
300300
}
301301

302+
/**
303+
* Sends an email notification to $receiver with given subject/content and optional attachments.
304+
* - Validates recipient email.
305+
* - Uses buildFromAddress() to construct a proper FROM (name + address).
306+
* - Attaches only OK-uploaded files.
307+
*/
302308
private function sendEmailNotification(User $receiver, User $sender, string $subject, string $content, array $attachmentList): void
303309
{
310+
// Validate recipient email early
304311
$toAddress = $receiver->getEmail();
305312
if (!filter_var($toAddress, FILTER_VALIDATE_EMAIL)) {
313+
// No valid recipient → nothing to send (could log if needed)
306314
return;
307315
}
308316

309-
$from = $this->buildFromAddress();
310-
311317
try {
312318
$email = (new Email())
313-
->from($from)
319+
->from($this->buildFromAddress())
314320
->to(new Address($toAddress, $receiver->getFullName() ?: $receiver->getUsername()))
315321
->subject($subject)
316322
->text($content)
317-
->html($content)
318-
;
323+
->html($content);
319324

325+
// Attach files if provided in the expected structure
320326
foreach ($attachmentList as $att) {
321327
$file = $att['file'] ?? null;
322328
if ($file instanceof UploadedFile && UPLOAD_ERR_OK === $file->getError()) {
@@ -325,24 +331,28 @@ private function sendEmailNotification(User $receiver, User $sender, string $sub
325331
}
326332

327333
$this->mailer->send($email);
328-
} catch (Throwable $e) {
329-
error_log('Failed to send email: '.$e->getMessage());
334+
} catch (\Throwable $e) {
335+
// Soft-fail: log and continue
336+
error_log('Failed to send email: ' . $e->getMessage());
330337
}
331338
}
332339

333340
/**
334-
* Constructs the FROM as an Address, prioritizing configuration;
335-
* If there is no valid value, infers the current domain and uses noreply@{domain}.
341+
* Builds the FROM address used in outgoing emails.
342+
* Priority (name): mail.mailer_from_name → platform.site_name → "Chamilo"
343+
* Priority (email): mail.mailer_from_email → platform.administrator_email → noreply@{host}
344+
* Host resolution: AccessUrl → RequestStack → 'example.org'
336345
*/
337346
private function buildFromAddress(): Address
338347
{
348+
// Resolve display name
339349
$fromName = $this->settingsManager->getSetting('mail.mailer_from_name')
340350
?: $this->settingsManager->getSetting('platform.site_name', true)
341351
?: 'Chamilo';
342352

353+
// Resolve email candidates (only existing/valid settings)
343354
$candidates = [
344355
$this->settingsManager->getSetting('mail.mailer_from_email'),
345-
$this->settingsManager->getSetting('mail.mailer_from_address'),
346356
$this->settingsManager->getSetting('platform.administrator_email'),
347357
];
348358
foreach ($candidates as $cand) {
@@ -351,6 +361,7 @@ private function buildFromAddress(): Address
351361
}
352362
}
353363

364+
// Fallback host inference
354365
$host = null;
355366
$accessUrl = $this->accessUrlHelper->getCurrent();
356367
if ($accessUrl && method_exists($accessUrl, 'getUrl')) {
@@ -365,7 +376,8 @@ private function buildFromAddress(): Address
365376
$host = 'example.org';
366377
}
367378

368-
return new Address('noreply@'.$host, $fromName);
379+
// Last-resort fallback
380+
return new Address('noreply@' . $host, $fromName);
369381
}
370382

371383
/**

0 commit comments

Comments
 (0)