From 3e739c2e66c18e0acb60e8bca988f62ad19992a0 Mon Sep 17 00:00:00 2001 From: Julio Date: Wed, 4 Dec 2019 09:06:22 +0100 Subject: [PATCH] Add Mailer component instead of swiftmailer --- .env | 12 +-- composer.json | 2 +- config/bundles.php | 1 - config/packages/dev/swiftmailer.yaml | 4 - config/packages/swiftmailer.yaml | 7 -- config/packages/test/swiftmailer.yaml | 2 - config/services.yaml | 7 +- public/main/inc/lib/api.lib.php | 129 +++++++------------------ src/CoreBundle/Framework/Container.php | 13 +-- 9 files changed, 45 insertions(+), 132 deletions(-) delete mode 100644 config/packages/dev/swiftmailer.yaml delete mode 100644 config/packages/swiftmailer.yaml delete mode 100644 config/packages/test/swiftmailer.yaml diff --git a/.env b/.env index b38b9e8a5b..73b7d418bc 100644 --- a/.env +++ b/.env @@ -10,14 +10,6 @@ DATABASE_USER='{{DATABASE_USER}}' DATABASE_PASSWORD='{{DATABASE_PASSWORD}}' ###< doctrine/doctrine-bundle ### -###> symfony/swiftmailer-bundle ### -MAILER_TRANSPORT='sendmail' -MAILER_USERNAME='' -MAILER_PASSWORD='' -MAILER_HOST='' -MAILER_PORT='' -###< symfony/swiftmailer-bundle ### - ###> symfony/framework-bundle ### APP_ENV='dev' APP_DEBUG='1' @@ -35,3 +27,7 @@ APP_LOCALE='en' APP_MULTIPLE_ACCESS_URL='' ###< chamilo ### + +###> symfony/mailer ### +MAILER_DSN=sendmail://localhost +###< symfony/mailer ### diff --git a/composer.json b/composer.json index 3d4c36e5c3..7898a315b6 100755 --- a/composer.json +++ b/composer.json @@ -70,9 +70,9 @@ "symfony/framework-bundle": "^4.0", "symfony/http-kernel": "^4.0", "symfony/monolog-bundle": "^3.1", + "symfony/mailer": "^4.0", "symfony/polyfill-apcu": "^1.9", "symfony/security-bundle": "^4.0", - "symfony/swiftmailer-bundle": "^3.2", "symfony/templating" : "^4.0", "symfony/translation": "^4.0", "symfony/twig-bundle": "^4.0", diff --git a/config/bundles.php b/config/bundles.php index 4b3c1b2dff..5842f4b432 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -8,7 +8,6 @@ return [ Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true], Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true], Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true], - Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true], Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true], WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle::class => ['all' => true], Sonata\EasyExtendsBundle\SonataEasyExtendsBundle::class => ['all' => true], diff --git a/config/packages/dev/swiftmailer.yaml b/config/packages/dev/swiftmailer.yaml deleted file mode 100644 index 5c36456a82..0000000000 --- a/config/packages/dev/swiftmailer.yaml +++ /dev/null @@ -1,4 +0,0 @@ -# See https://symfony.com/doc/current/email/dev_environment.html -swiftmailer: - # send all emails to a specific address - #delivery_addresses: ['me@example.com'] diff --git a/config/packages/swiftmailer.yaml b/config/packages/swiftmailer.yaml deleted file mode 100644 index ebda80009a..0000000000 --- a/config/packages/swiftmailer.yaml +++ /dev/null @@ -1,7 +0,0 @@ -swiftmailer: - transport: '%env(MAILER_TRANSPORT)%' - username: '%env(MAILER_USERNAME)%' - password: '%env(MAILER_PASSWORD)%' - host: '%env(MAILER_HOST)%' - port: '%env(MAILER_PORT)%' - spool: {type: 'memory'} diff --git a/config/packages/test/swiftmailer.yaml b/config/packages/test/swiftmailer.yaml deleted file mode 100644 index f43807805e..0000000000 --- a/config/packages/test/swiftmailer.yaml +++ /dev/null @@ -1,2 +0,0 @@ -swiftmailer: - disable_delivery: true diff --git a/config/services.yaml b/config/services.yaml index aca24bcb77..d898a4251f 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -10,10 +10,6 @@ parameters: resources: - 'GeneratorBundle::fields.html.twig' - mailer_transport: smtp - mailer_host: 127.0.0.1 - mailer_user: admin@example.com - mailer_password: null course_info_is_not_editable: false sonata_media.cdn.host: '/var/upload/media/' sonata_page.varnish.command: 'if [ ! -r "/etc/varnish/secret" ]; then echo "VALID ERROR :/"; else varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 {{ COMMAND }} "{{ EXPRESSION }}"; fi;' @@ -79,6 +75,9 @@ services: - '@Chamilo\CoreBundle\Repository\ResourceFactory' - '@Chamilo\CoreBundle\Block\BreadcrumbBlockService' + Symfony\Component\Mailer\Mailer: + public: true + Chamilo\ThemeBundle\: autowire: true resource: '../src/ThemeBundle/*' diff --git a/public/main/inc/lib/api.lib.php b/public/main/inc/lib/api.lib.php index 779c67953e..8ccc903609 100644 --- a/public/main/inc/lib/api.lib.php +++ b/public/main/inc/lib/api.lib.php @@ -11,8 +11,11 @@ use Chamilo\CourseBundle\Entity\CItemProperty; use Chamilo\ThemeBundle\Controller\ExceptionController; use Chamilo\UserBundle\Entity\User; use ChamiloSession as Session; +use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\Finder\Finder; +use Symfony\Component\Mime\Address; +use Symfony\Component\Mime\Email; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; /** @@ -1845,7 +1848,7 @@ function api_get_anonymous_id() $ip = Database::escape_string(api_get_real_ip()); $max = (int) api_get_configuration_value('max_anonymous_users'); if ($max >= 2) { - $sql = "SELECT * FROM $table as TEL + $sql = "SELECT * FROM $table as TEL JOIN $tableU as U ON U.user_id = TEL.login_user_id WHERE TEL.user_ip = '$ip' @@ -1880,8 +1883,8 @@ function api_get_anonymous_id() } $table = Database::get_main_table(TABLE_MAIN_USER); - $sql = "SELECT user_id - FROM $table + $sql = "SELECT user_id + FROM $table WHERE status = ".ANONYMOUS." "; $res = Database::query($sql); if (Database::num_rows($res) > 0) { @@ -4679,7 +4682,7 @@ function api_display_language_form($hide_if_no_choice = false, $showAsButton = f } else { $html = ' - + '.$currentLanguageInfo['original_name'].' @@ -4795,7 +4798,7 @@ function languageToCountryIsoCode($languageIsoCode) function api_get_languages() { $table = Database::get_main_table(TABLE_MAIN_LANGUAGE); - $sql = "SELECT * FROM $table WHERE available='1' + $sql = "SELECT * FROM $table WHERE available='1' ORDER BY original_name ASC"; $result = Database::query($sql); $languages = []; @@ -8027,8 +8030,8 @@ function api_get_password_checker_js($usernameInputId, $passwordInputId) }; $(function() { - var lang = ".json_encode($translations)."; - var options = { + var lang = ".json_encode($translations)."; + var options = { onLoad : function () { //$('#messages').text('Start typing password'); }, @@ -8046,7 +8049,7 @@ function api_get_password_checker_js($usernameInputId, $passwordInputId) options.i18n = { t: function (key) { var result = lang[key]; - return result === key ? '' : result; // This assumes you return the + return result === key ? '' : result; // This assumes you return the } }; $('".$passwordInputId."').pwstrength(options); @@ -8717,57 +8720,14 @@ function api_mail_html( $replyToName = $extra_headers['reply_to']['name']; } - //If the SMTP configuration only accept one sender - /*if (isset($platform_email['SMTP_UNIQUE_SENDER']) && $platform_email['SMTP_UNIQUE_SENDER']) { - $senderName = $platform_email['SMTP_FROM_NAME']; - $senderEmail = $platform_email['SMTP_FROM_EMAIL']; - $valid = PHPMailer::validateAddress($senderEmail); - if ($valid) { - //force-set Sender to $senderEmail, otherwise SetFrom only does it if it is currently empty - $mail->Sender = $senderEmail; - } - }*/ - - /*$mail->SetFrom($senderEmail, $senderName); - $mail->Subject = $subject; - $mail->AltBody = strip_tags( - str_replace('
', "\n", api_html_entity_decode($message)) - );*/ - - /*if (is_array($extra_headers) && count($extra_headers) > 0) { - foreach ($extra_headers as $key => $value) { - switch (strtolower($key)) { - case 'encoding': - case 'content-transfer-encoding': - $mail->Encoding = $value; - break; - case 'charset': - $mail->Charset = $value; - break; - case 'contenttype': - case 'content-type': - $mail->ContentType = $value; - break; - default: - $mail->AddCustomHeader($key.':'.$value); - break; - } - } - } else { - if (!empty($extra_headers)) { - $mail->AddCustomHeader($extra_headers); - } - }*/ - - // WordWrap the html body (phpMailer only fixes AltBody) FS#2988 - //$mail->Body = $mail->WrapText($mail->Body, $mail->WordWrap); try { - $message = new \Swift_Message($subject); + $message = new TemplatedEmail(); + $message->subject($subject); $list = api_get_configuration_value('send_all_emails_to'); if (!empty($list) && isset($list['emails'])) { foreach ($list['emails'] as $email) { - $message->addCc($email); + $message->cc($email); } } @@ -8775,10 +8735,7 @@ function api_mail_html( if (!empty($data_file)) { foreach ($data_file as $file_attach) { if (!empty($file_attach['path']) && !empty($file_attach['filename'])) { - //$message->attach(Swift_Attachment::fromPath($file_attach['path'], $file_attach['filename']); - $message->attach( - Swift_Attachment::fromPath($file_attach['path'])->setFilename($file_attach['filename']) - ); + $message->attachFromPath($file_attach['path'], $file_attach['filename']); } } } @@ -8800,41 +8757,27 @@ function api_mail_html( $paramsHtml = $paramsText = $params; $paramsHtml['content'] = $body; - $paramsText['content'] = str_replace('
', "\n", api_html_entity_decode($body)); + //$paramsText['content'] = str_replace('
', "\n", api_html_entity_decode($body)); if (!empty($senderEmail)) { - $message->setFrom([$senderEmail => $senderName]); + $message->from(new Address($senderEmail, $senderName)); } if (!empty($recipientEmail)) { - $message->setTo([$recipientEmail => $recipientName]); + $message->to(new Address($recipientEmail, $recipientName)); } if (!empty($replyToEmail)) { - $message->setReplyTo([$replyToEmail => $replyToName]); + $message->replyTo(new Address($replyToEmail, $replyToName)); } $message - ->setBody( - Container::getTwig()->render( - 'ChamiloThemeBundle:Mailer:Default/default.html.twig', - $paramsHtml - ), - 'text/html' - ) - ->addPart( - Container::getTwig()->render( - 'ChamiloThemeBundle:Mailer:Default/default.text.twig', - $paramsText - ), - 'text/plain' - ) - //->setEncoder(\Swift_Encoding::get8BitEncoding()) + ->htmlTemplate('ChamiloThemeBundle:Mailer:Default/default.html.twig') + ->textTemplate('ChamiloThemeBundle:Mailer:Default/default.text.twig') ; - - $type = $message->getHeaders()->get('Content-Type'); - $type->setCharset('utf-8'); - Container::getMailer()->send($message); + $message->context($paramsHtml); + $result = Container::getMailer()->send($message); + var_dump($result);exit; return true; } catch (Exception $e) { @@ -9325,7 +9268,7 @@ function api_find_template($template) function api_get_language_list_for_flag() { $table = Database::get_main_table(TABLE_MAIN_LANGUAGE); - $sql = "SELECT english_name, isocode FROM $table + $sql = "SELECT english_name, isocode FROM $table ORDER BY original_name ASC"; static $languages = []; if (empty($languages)) { @@ -9373,11 +9316,11 @@ function api_get_language_translate_html() $hideAll .= ' $("span:lang('.$language['isocode'].')").filter( function(e, val) { - // Only find the spans if they have set the lang - if ($(this).attr("lang") == null) { + // Only find the spans if they have set the lang + if ($(this).attr("lang") == null) { return false; } - + // Ignore ckeditor classes return !this.className.match(/cke(.*)/); }).hide();'."\n"; @@ -9394,32 +9337,32 @@ function api_get_language_translate_html() return ' $(function() { - '.$hideAll.' - var defaultLanguageFromUser = "'.$isoCode.'"; - + '.$hideAll.' + var defaultLanguageFromUser = "'.$isoCode.'"; + $("span:lang('.$isoCode.')").filter( function() { // Ignore ckeditor classes return !this.className.match(/cke(.*)/); }).show(); - + var defaultLanguage = ""; var langFromUserFound = false; - + $(this).find("span").filter( function() { // Ignore ckeditor classes return !this.className.match(/cke(.*)/); }).each(function() { - defaultLanguage = $(this).attr("lang"); + defaultLanguage = $(this).attr("lang"); if (defaultLanguage) { - $(this).before().next("br").remove(); + $(this).before().next("br").remove(); if (defaultLanguageFromUser == defaultLanguage) { langFromUserFound = true; } } }); - + // Show default language if (langFromUserFound == false && defaultLanguage) { $("span:lang("+defaultLanguage+")").filter( diff --git a/src/CoreBundle/Framework/Container.php b/src/CoreBundle/Framework/Container.php index 9a22425b66..57d9defd3c 100644 --- a/src/CoreBundle/Framework/Container.php +++ b/src/CoreBundle/Framework/Container.php @@ -229,20 +229,9 @@ class Container return false; } - /** - * @return AssetsHelper - */ - public static function getAsset() - { - return self::$container->get('templating.helper.assets'); - } - - /** - * @return \Swift_Mailer - */ public static function getMailer() { - return self::$container->get('mailer'); + return self::$container->get('Symfony\Component\Mailer\Mailer'); } /**