Skip to content

Internal: Use fallback_user as email sender and set Reply-To to actual sender - refs #4283 #6535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 32 additions & 16 deletions src/CoreBundle/Helpers/MessageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;

use const PHP_SAPI;
Expand Down Expand Up @@ -295,40 +296,55 @@ private function addSenderAsReceiver(Message $message, User $sender): void
}
}

private function sendEmailNotification(User $receiver, User $sender, string $subject, string $content, array $attachmentList): void
{
if (empty($receiver->getEmail())) {
throw new Exception('The receiver does not have a valid email address.');
}

private function sendEmailNotification(
User $receiver,
User $sender,
string $subject,
string $content,
array $attachmentList
): void {
// 1. Attempt to get the "from" email and name from settings
$smtpFromEmail = $this->settingsManager->getSetting('mail.mailer_from_name');

if (empty($smtpFromEmail)) {
$smtpFromEmail = $this->settingsManager->getSetting('platform.administrator_email');
}

if (empty($smtpFromEmail)) {
$smtpFromEmail = '[email protected]';
$smtpFromName = $this->settingsManager->getSetting('platform.administrator_email');

// 2. If no valid "from" is configured, fall back to the system user
if (empty($smtpFromEmail) || empty($smtpFromName)) {
$fallback = $this->userRepository->getFallbackUser();
if ($fallback) {
$smtpFromEmail = $fallback->getEmail();
$smtpFromName = $fallback->getFullname();
} else {
$smtpFromEmail = '[email protected]';
$smtpFromName = 'Chamilo Platform';
}
}

// 3. Build the email including the display name
$email = (new Email())
->from($smtpFromEmail)
->from(new Address($smtpFromEmail, $smtpFromName))
// 4. Set Reply-To so replies go to the actual sender
->replyTo(new Address($sender->getEmail(), $sender->getFullname()))
->to($receiver->getEmail())
->subject($subject)
->text($content)
->html($content)
;

// 5. Attach any uploaded files
foreach ($attachmentList as $attachment) {
if ($attachment instanceof UploadedFile) {
$email->attachFromPath($attachment->getRealPath(), $attachment->getClientOriginalName());
$email->attachFromPath(
$attachment->getRealPath(),
$attachment->getClientOriginalName()
);
}
}

// 6. Send and log any failures
try {
$this->mailer->send($email);
} catch (Exception $e) {
error_log('Failed to send email: '.$e->getMessage());
error_log('Failed to send email: ' . $e->getMessage());
}
}

Expand Down
Loading