Merge pull request #5462 from AngelFQC/BT21613
Internal: Restoring mail conf variablespull/5539/head
commit
119efed26f
@ -1,3 +0,0 @@ |
||||
framework: |
||||
mailer: |
||||
dsn: 'null://null' |
||||
@ -1,3 +1,13 @@ |
||||
framework: |
||||
mailer: |
||||
dsn: '%env(MAILER_DSN)%' |
||||
|
||||
when@dev: |
||||
framework: |
||||
mailer: |
||||
dsn: 'null://null' |
||||
|
||||
when@test: |
||||
framework: |
||||
mailer: |
||||
dsn: 'null://null' |
||||
|
||||
@ -1,3 +0,0 @@ |
||||
framework: |
||||
mailer: |
||||
dsn: 'null://null' |
||||
@ -1,4 +0,0 @@ |
||||
framework: |
||||
messenger: |
||||
transports: |
||||
sync_priority_high: 'in-memory://' |
||||
@ -1,38 +0,0 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Entity\Listener; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Message; |
||||
use Doctrine\ORM\Event\PostPersistEventArgs; |
||||
use Symfony\Component\Messenger\MessageBusInterface; |
||||
|
||||
class MessageListener |
||||
{ |
||||
public function __construct( |
||||
private MessageBusInterface $bus |
||||
) {} |
||||
|
||||
public function postPersist(Message $message, PostPersistEventArgs $args): void |
||||
{ |
||||
if ($message) { |
||||
// Creates an outbox version, if message is sent in the inbox. |
||||
if (Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) { |
||||
/*$messageSent = clone $message; |
||||
$messageSent |
||||
->setMsgType(Message::MESSAGE_TYPE_OUTBOX) |
||||
->setRead(true) |
||||
->setReceivers(null) |
||||
; |
||||
$args->getEntityManager()->persist($messageSent); |
||||
$args->getEntityManager()->flush();*/ |
||||
|
||||
// Dispatch to the Messenger bus. Function MessageHandler.php will send the message. |
||||
$this->bus->dispatch($message); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -1,62 +0,0 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\MessageHandler; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Message; |
||||
use Chamilo\CoreBundle\Entity\MessageRelUser; |
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
||||
use Symfony\Component\Mailer\Mailer; |
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; |
||||
use Symfony\Component\Mime\Address; |
||||
|
||||
#[AsMessageHandler] |
||||
class MessageHandler |
||||
{ |
||||
public function __construct( |
||||
private readonly Mailer $mailer |
||||
) {} |
||||
public function __invoke(Message $message): void |
||||
{ |
||||
if (Message::MESSAGE_TYPE_INBOX !== $message->getMsgType()) { |
||||
// Only send messages to the inbox. |
||||
return; |
||||
} |
||||
|
||||
$email = (new TemplatedEmail()) |
||||
->subject($message->getTitle()) |
||||
->from(new Address($message->getSender()->getEmail(), $message->getSender()->getFirstname())) |
||||
->htmlTemplate('@ChamiloCore/Mailer/Default/default.html.twig') |
||||
->textTemplate('@ChamiloCore/Mailer/Default/default.text.twig') |
||||
; |
||||
foreach ($message->getReceivers() as $messageRelUser) { |
||||
$receiver = $messageRelUser->getReceiver(); |
||||
$address = new Address($receiver->getEmail(), $receiver->getFirstname()); |
||||
|
||||
if (MessageRelUser::TYPE_TO === $messageRelUser->getReceiverType()) { |
||||
$email->addTo($address); |
||||
} elseif (MessageRelUser::TYPE_CC === $messageRelUser->getReceiverType()) { |
||||
$email->addBcc($address); |
||||
} |
||||
} |
||||
|
||||
$params = [ |
||||
'content' => $message->getContent(), |
||||
'automatic_email_text' => '', |
||||
'mail_header_style' => '', |
||||
'mail_content_style' => '', |
||||
'theme' => '', |
||||
]; |
||||
$email->context($params); |
||||
|
||||
try { |
||||
$this->mailer->send($email); |
||||
} catch (TransportExceptionInterface $e) { |
||||
error_log('MessageHandler exception: '.$e->getMessage()); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,31 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
class Version20240506164100 extends AbstractMigrationChamilo |
||||
{ |
||||
/** |
||||
* @inheritDoc |
||||
*/ |
||||
public function up(Schema $schema): void |
||||
{ |
||||
$selectedMailValue = $this->getMailConfigurationValueFromFile('SMTP_UNIQUE_SENDER') ? 'true' : 'false'; |
||||
|
||||
$this->addSql("INSERT INTO settings_current (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext, access_url, access_url_changeable, access_url_locked) VALUES ('smtp_unique_sender', null, null, 'mail', '$selectedMailValue', 'smtp_unique_sender', null, '', null, 1, 1, 1)"); |
||||
|
||||
$selectedMailValue = $this->getMailConfigurationValueFromFile('SMTP_FROM_EMAIL'); |
||||
|
||||
$this->addSql("INSERT INTO settings_current (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext, access_url, access_url_changeable, access_url_locked) VALUES ('smtp_from_email', null, null, 'mail', '$selectedMailValue', 'smtp_from_email', null, '', null, 1, 1, 1)"); |
||||
|
||||
$selectedMailValue = $this->getMailConfigurationValueFromFile('SMTP_FROM_NAME'); |
||||
|
||||
$this->addSql("INSERT INTO settings_current (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext, access_url, access_url_changeable, access_url_locked) VALUES ('smtp_from_name', null, null, 'mail', '$selectedMailValue', 'smtp_from_name', null, '', null, 1, 1, 1)"); |
||||
} |
||||
} |
||||
@ -1,3 +0,0 @@ |
||||
{{ automatic_email_text }} |
||||
|
||||
{{ content }} |
||||
@ -0,0 +1,122 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\ServiceHelper; |
||||
|
||||
use Exception; |
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
||||
use Symfony\Component\Mailer\MailerInterface; |
||||
use Symfony\Component\Mime\Address; |
||||
use Symfony\Component\Mime\BodyRendererInterface; |
||||
|
||||
final class MailHelper |
||||
{ |
||||
public function __construct( |
||||
private readonly MailerInterface $mailer, |
||||
private readonly BodyRendererInterface $bodyRenderer, |
||||
) {} |
||||
|
||||
public function send( |
||||
string $recipientName, |
||||
string $recipientEmail, |
||||
string $subject, |
||||
string $body, |
||||
?string $senderName = null, |
||||
?string $senderEmail = null, |
||||
array $extra_headers = [], |
||||
array $data_file = [], |
||||
bool $embeddedImage = false, |
||||
array $additionalParameters = [], |
||||
?string $sendErrorTo = null, |
||||
): bool { |
||||
if (!api_valid_email($recipientEmail)) { |
||||
return false; |
||||
} |
||||
|
||||
$templatedEmail = new TemplatedEmail(); |
||||
|
||||
api_set_noreply_and_from_address_to_mailer( |
||||
$templatedEmail, |
||||
['name' => $senderName, 'email' => $senderEmail], |
||||
!empty($extra_headers['reply_to']) ? $extra_headers['reply_to'] : [] |
||||
); |
||||
|
||||
if ($sendErrorTo) { |
||||
$templatedEmail |
||||
->getHeaders() |
||||
->addIdHeader('Errors-To', $sendErrorTo) |
||||
; |
||||
} |
||||
|
||||
// Reply to first |
||||
$replyToName = ''; |
||||
$replyToEmail = ''; |
||||
if (isset($extra_headers['reply_to'])) { |
||||
$replyToEmail = $extra_headers['reply_to']['mail']; |
||||
$replyToName = $extra_headers['reply_to']['name']; |
||||
} |
||||
|
||||
try { |
||||
$templatedEmail->subject($subject); |
||||
|
||||
$list = api_get_setting('announcement.send_all_emails_to', true); |
||||
|
||||
if (!empty($list) && isset($list['emails'])) { |
||||
foreach ($list['emails'] as $email) { |
||||
$templatedEmail->cc($email); |
||||
} |
||||
} |
||||
|
||||
// Attachment |
||||
if (!empty($data_file)) { |
||||
foreach ($data_file as $file_attach) { |
||||
if (!empty($file_attach['path']) && !empty($file_attach['filename'])) { |
||||
$templatedEmail->attachFromPath($file_attach['path'], $file_attach['filename']); |
||||
} |
||||
} |
||||
} |
||||
|
||||
$noReply = api_get_setting('noreply_email_address'); |
||||
$automaticEmailText = ''; |
||||
|
||||
if (!empty($noReply)) { |
||||
$automaticEmailText = '<br />'.get_lang('This is an automatic email message. Please do not reply to it.'); |
||||
} |
||||
|
||||
$params = [ |
||||
'mail_header_style' => api_get_setting('mail.mail_header_style'), |
||||
'mail_content_style' => api_get_setting('mail.mail_content_style'), |
||||
'link' => $additionalParameters['link'] ?? '', |
||||
'automatic_email_text' => $automaticEmailText, |
||||
'content' => $body, |
||||
'theme' => api_get_visual_theme(), |
||||
]; |
||||
|
||||
if (!empty($recipientEmail)) { |
||||
$templatedEmail->to(new Address($recipientEmail, $recipientName)); |
||||
} |
||||
|
||||
if (!empty($replyToEmail)) { |
||||
$templatedEmail->replyTo(new Address($replyToEmail, $replyToName)); |
||||
} |
||||
|
||||
$templatedEmail |
||||
->htmlTemplate('@ChamiloCore/Mailer/Default/default.html.twig') |
||||
->context($params) |
||||
; |
||||
|
||||
$this->bodyRenderer->render($templatedEmail); |
||||
$this->mailer->send($templatedEmail); |
||||
|
||||
return true; |
||||
} catch (Exception|TransportExceptionInterface $e) { |
||||
error_log($e->getMessage()); |
||||
|
||||
return false; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,63 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\State; |
||||
|
||||
use ApiPlatform\Metadata\DeleteOperationInterface; |
||||
use ApiPlatform\Metadata\Operation; |
||||
use ApiPlatform\Metadata\Post; |
||||
use ApiPlatform\State\ProcessorInterface; |
||||
use Chamilo\CoreBundle\Entity\Message; |
||||
use Notification; |
||||
|
||||
final class MessageProcessor implements ProcessorInterface |
||||
{ |
||||
public function __construct( |
||||
private readonly ProcessorInterface $persistProcessor, |
||||
private readonly ProcessorInterface $removeProcessor, |
||||
) {} |
||||
|
||||
public function process($data, Operation $operation, array $uriVariables = [], array $context = []) |
||||
{ |
||||
if ($operation instanceof DeleteOperationInterface) { |
||||
return $this->removeProcessor->process($data, $operation, $uriVariables, $context); |
||||
} |
||||
|
||||
$message = $this->persistProcessor->process($data, $operation, $uriVariables, $context); |
||||
|
||||
assert($message instanceof Message); |
||||
|
||||
if ($operation instanceof Post) { |
||||
if (Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) { |
||||
$this->saveNotificationForInboxMessage($message); |
||||
} |
||||
} |
||||
|
||||
return $message; |
||||
} |
||||
|
||||
private function saveNotificationForInboxMessage(Message $message): void |
||||
{ |
||||
$sender_info = api_get_user_info( |
||||
$message->getSender()->getId() |
||||
); |
||||
|
||||
foreach ($message->getReceivers() as $receiver) { |
||||
$user = $receiver->getReceiver(); |
||||
|
||||
(new Notification()) |
||||
->saveNotification( |
||||
$message->getId(), |
||||
Notification::NOTIFICATION_TYPE_MESSAGE, |
||||
[$user->getId()], |
||||
$message->getTitle(), |
||||
$message->getContent(), |
||||
$sender_info, |
||||
) |
||||
; |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue