swiftmailer replaced by mailerinterface

This commit is contained in:
Julie Lenaerts 2022-07-25 15:41:44 +02:00
parent 54ffa999d8
commit 941d7b0352
2 changed files with 20 additions and 39 deletions

View File

@ -13,8 +13,9 @@ namespace Chill\MainBundle\Notification;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Swift_Mailer;
use Swift_Message; use Swift_Message;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment; use Twig\Environment;
@ -29,40 +30,19 @@ use function call_user_func;
*/ */
class Mailer class Mailer
{ {
/** protected MailerInterface $forcedMailer;
* @var Swift_Mailer
*/
protected $forcedMailer;
/** protected LoggerInterface $logger;
* @var LoggerInterface
*/
protected $logger;
/** protected MailerInterface $mailer;
* @var Swift_Mailer
*/
protected $mailer;
/** protected array $routeParameters;
* @var array
*/
protected $routeParameters;
/** protected RouterInterface $router;
* @var RouterInterface
*/
protected $router;
/** protected TranslatorInterface $translator;
* @var TranslatorInterface
*/
protected $translator;
/** protected Environment $twig;
* @var \Twig\Environment
*/
protected $twig;
/** /**
* Mailer constructor. * Mailer constructor.
@ -72,7 +52,7 @@ class Mailer
public function __construct( public function __construct(
LoggerInterface $logger, LoggerInterface $logger,
Environment $twig, Environment $twig,
Swift_Mailer $mailer, MailerInterface $mailer,
// due to bug https://github.com/symfony/swiftmailer-bundle/issues/127 // due to bug https://github.com/symfony/swiftmailer-bundle/issues/127
// \Swift_Transport $mailerTransporter, // \Swift_Transport $mailerTransporter,
RouterInterface $router, RouterInterface $router,
@ -120,12 +100,12 @@ class Mailer
* *
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/ */
public function sendMessage(Swift_Message $message, $force) public function sendMessage(Email $email, $force)
{ {
if ($force) { if ($force) {
$this->forcedMailer->send($message); $this->forcedMailer->send($email);
} else { } else {
$this->mailer->send($message); $this->mailer->send($email);
} }
} }
@ -146,7 +126,7 @@ class Mailer
$force = false $force = false
) { ) {
$fromEmail = $this->routeParameters['from_email']; $fromEmail = $this->routeParameters['from_email'];
$fromName = $this->routeParameters['from_name']; // $fromName = $this->routeParameters['from_name'];
$to = $recipient instanceof User ? $recipient->getEmail() : $recipient; $to = $recipient instanceof User ? $recipient->getEmail() : $recipient;
$subjectI18n = $this->translator->trans( $subjectI18n = $this->translator->trans(
@ -155,12 +135,13 @@ class Mailer
$subject[2] ?? null $subject[2] ?? null
); );
$message = (new Swift_Message($subjectI18n)) $message = (new Email());
->setFrom($fromEmail, $fromName) $message->subject($subjectI18n);
->setTo($to); $message->to($to);
$message->from($fromEmail);
foreach ($bodies as $contentType => $content) { foreach ($bodies as $contentType => $content) {
$message->setBody($content, $contentType); $message->text($content);
} }
if (null !== $callback) { if (null !== $callback) {

View File

@ -12,7 +12,7 @@ services:
arguments: arguments:
$logger: '@Psr\Log\LoggerInterface' $logger: '@Psr\Log\LoggerInterface'
$twig: '@Twig\Environment' $twig: '@Twig\Environment'
$mailer: '@swiftmailer.mailer.default' $mailer: '@Symfony\Component\Mailer\MailerInterface'
# $mailerTransporter: '@swiftmailer.transport' # $mailerTransporter: '@swiftmailer.transport'
$router: '@Symfony\Component\Routing\RouterInterface' $router: '@Symfony\Component\Routing\RouterInterface'
$translator: '@Symfony\Contracts\Translation\TranslatorInterface' $translator: '@Symfony\Contracts\Translation\TranslatorInterface'