Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/CoreBundle/Helpers/MessageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
->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()) {
Expand All @@ -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) {
Expand All @@ -351,6 +361,7 @@ private function buildFromAddress(): Address
}
}

// Fallback host inference
$host = null;
$accessUrl = $this->accessUrlHelper->getCurrent();
if ($accessUrl && method_exists($accessUrl, 'getUrl')) {
Expand All @@ -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);
}

/**
Expand Down
Loading