diff --git a/.env b/.env index d1b3b36..dcac2e3 100644 --- a/.env +++ b/.env @@ -15,9 +15,13 @@ ###> symfony/framework-bundle ### APP_ENV=dev -APP_SECRET=e6e287f176fe2c69112fc620e1801bf0 +APP_SECRET= ###< symfony/framework-bundle ### +###> symfony/mailer ### +MAILER_DSN=smtp://localhost +###< symfony/mailer ### + ###> doctrine/doctrine-bundle ### # Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml @@ -26,10 +30,3 @@ DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" # DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7" # DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8" ###< doctrine/doctrine-bundle ### - -###> symfony/swiftmailer-bundle ### -# For Gmail as a transport, use: "gmail://username:password@localhost" -# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" -# Delivery is disabled by default via "null://localhost" -MAILER_URL=null://localhost -###< symfony/swiftmailer-bundle ### diff --git a/composer.json b/composer.json index de5e0af..8621344 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,6 @@ "symfony/security-bundle": "5.4.*", "symfony/serializer": "5.4.*", "symfony/string": "5.4.*", - "symfony/swiftmailer-bundle": "^3.5", "symfony/translation": "5.4.*", "symfony/twig-bundle": "^5.2", "symfony/validator": "5.4.*", diff --git a/config/bundles.php b/config/bundles.php index ace76a4..15f9428 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -13,7 +13,6 @@ return [ Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true], Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true], Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true], - Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true], Scheb\TwoFactorBundle\SchebTwoFactorBundle::class => ['all' => true], Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle::class => ['all' => true], Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true], diff --git a/config/packages/dev/swiftmailer.yaml b/config/packages/dev/swiftmailer.yaml deleted file mode 100644 index b98158e..0000000 --- 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/mailer.yaml b/config/packages/mailer.yaml index 56a650d..d6863f8 100644 --- a/config/packages/mailer.yaml +++ b/config/packages/mailer.yaml @@ -1,3 +1,5 @@ framework: mailer: dsn: '%env(MAILER_DSN)%' + headers: + From: 'Example ' diff --git a/config/packages/swiftmailer.yaml b/config/packages/swiftmailer.yaml deleted file mode 100644 index c06f0bd..0000000 --- a/config/packages/swiftmailer.yaml +++ /dev/null @@ -1,4 +0,0 @@ -swiftmailer: - url: '%env(MAILER_URL)%' - spool: { type: 'memory' } - sender_address: system@localhost diff --git a/config/packages/test/swiftmailer.yaml b/config/packages/test/swiftmailer.yaml deleted file mode 100644 index f438078..0000000 --- a/config/packages/test/swiftmailer.yaml +++ /dev/null @@ -1,2 +0,0 @@ -swiftmailer: - disable_delivery: true diff --git a/core/Notification/MailNotifier.php b/core/Notification/MailNotifier.php index 63aefd4..c4fc96a 100644 --- a/core/Notification/MailNotifier.php +++ b/core/Notification/MailNotifier.php @@ -2,9 +2,8 @@ namespace App\Core\Notification; -use Swift_Attachment; -use Swift_Mailer; -use Swift_Message; +use Symfony\Bridge\Twig\Mime\TemplatedEmail; +use Symfony\Component\Mailer\MailerInterface; use Twig\Environment as TwigEnvironment; /** @@ -14,56 +13,20 @@ use Twig\Environment as TwigEnvironment; */ class MailNotifier { - /** - * @var Swift_Mailer - */ - protected $mailer; + protected MailerInterface $mailer; + protected array $attachments = []; + protected array $recipients = []; + protected array $bccRecipients = []; + protected ?string $subject = null; + protected ?string $from = null; + protected ?string $replyTo = null; - /** - * @var array - */ - protected $attachments = []; - - /** - * @var array - */ - protected $recipients = []; - - /** - * @var array - */ - protected $bccRecipients = []; - - /** - * @var string - */ - protected $subject; - - /** - * @var string - */ - protected $from; - - /** - * @var string - */ - protected $replyTo; - - /** - * Constructor. - * - * @param BasicNotifier $basicNotifier - * @param Swift_Mailer $mail - */ - public function __construct(TwigEnvironment $twig, Swift_Mailer $mailer) + public function __construct(TwigEnvironment $twig, MailerInterface $mailer) { $this->mailer = $mailer; $this->twig = $twig; } - /** - * @return EmailNotifier - */ public function setMailer(Swift_Mailer $mailer): self { $this->mailer = $mailer; @@ -76,9 +39,6 @@ class MailNotifier return $this->mailer; } - /** - * @return EmailNotifier - */ public function setRecipients(array $recipients): self { $this->recipients = $recipients; @@ -91,9 +51,6 @@ class MailNotifier return $this->recipients; } - /** - * @return EmailNotifier - */ public function setBccRecipients(array $bccRecipients): self { $this->bccRecipients = $bccRecipients; @@ -106,11 +63,6 @@ class MailNotifier return $this->bccRecipients; } - /** - * @param string $subject - * - * @return EmailNotifier - */ public function setSubject(?string $subject): self { $this->subject = $subject; @@ -123,11 +75,6 @@ class MailNotifier return $this->subject; } - /** - * @param mixed $from - * - * @return EmailNotifier - */ public function setFrom($from): self { $this->from = $from; @@ -135,21 +82,11 @@ class MailNotifier return $this; } - /** - * @return mixed - */ public function getFrom(): ?string { return $this->from; } - /** - * Set the value of "replyTo". - * - * @param string $replyTo - * - * @return EmailNotifier - */ public function setReplyTo($replyTo): self { $this->replyTo = $replyTo; @@ -157,19 +94,11 @@ class MailNotifier return $this; } - /* - * Get the value of "replyTo". - * - * @return string - */ public function getReplyTo(): ?string { return $this->replyTo; } - /** - * @return EmailNotifier - */ public function setAttachments(array $attachments): self { $this->attachments = $attachments; @@ -182,9 +111,6 @@ class MailNotifier return $this->attachments; } - /** - * @return EmailNotifier - */ public function addRecipient(string $email, bool $isBcc = false): self { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { @@ -204,9 +130,6 @@ class MailNotifier return $this; } - /** - * @return EmailNotifier - */ public function addRecipients(array $emails, bool $isBcc = false): self { foreach ($emails as $email) { @@ -216,19 +139,11 @@ class MailNotifier return $this; } - /** - * @return EmailNotifier - */ public function addRecipientByAccount(Account $account, bool $isBcc = false): self { return $this->addRecipient($account->getEmail(), $isBcc); } - /** - * @param mixed $accounts - * - * @return EmailNotifier - */ public function addRecipientsByAccounts($accounts, bool $isBcc = false) { if (!is_array($accounts)) { @@ -242,9 +157,6 @@ class MailNotifier return $this; } - /** - * @return EmailNotifier - */ public function addAttachment(string $attachment): self { if (!in_array($attachment, $this->attachments)) { @@ -254,9 +166,6 @@ class MailNotifier return $this; } - /** - * @return EmailNotifier - */ public function addAttachments(array $attachments): self { foreach ($attachments as $attachment) { @@ -266,9 +175,6 @@ class MailNotifier return $this; } - /** - * @return EmailNotifier - */ public function init(): self { $this @@ -281,58 +187,50 @@ class MailNotifier return $this; } - /** - * @return EmailNotifier - */ public function notify(string $template, array $data = [], string $type = 'text/html'): self { - $message = $this->createMessage( - $this->twig->render( - $template, - $data - ), - $type - ); + $message = $this->createMessage(); + $message->context($data); + + if (in_array($type, ['text/plain', 'text'])) { + $message->textTemplate($template); + } else { + $message->htmlTemplate($template); + } $this->mailer->send($message); return $this; } - protected function createMessage(string $body, string $type = 'text/html'): Swift_Message + protected function createMessage(): TemplatedEmail { - $message = new Swift_Message(); + $message = new TemplatedEmail(); if ($this->getSubject()) { - $message->setSubject($this->getSubject()); + $message->subject($this->getSubject()); } if ($this->getFrom()) { - $message->setFrom($this->getFrom()); + $message->from($this->getFrom()); } if ($this->getReplyTo()) { - $message->setReplyTo($this->getReplyTo()); + $message->replyTo($this->getReplyTo()); } if (count($this->getRecipients()) > 0) { - $message->setTo($this->getRecipients()); + $message->to(...$this->getRecipients()); } if (count($this->getBccRecipients()) > 0) { - $message->setBcc($this->getBccRecipients()); + $message->bcc(...$this->getBccRecipients()); } foreach ($this->getAttachments() as $attachment) { - if (is_object($attachment) && $attachment instanceof Swift_Attachment) { - $message->attach($attachment); - } elseif (is_string($attachment) && file_exists($attachment) && is_readable($attachment) && !is_dir($attachment)) { - $message->attach(Swift_Attachment::fromPath($attachment)); - } + $message->attachFromPath($attachment); } - $message->setBody($body, $type); - return $message; } } diff --git a/symfony.lock b/symfony.lock index 9393b2f..7e97f5e 100644 --- a/symfony.lock +++ b/symfony.lock @@ -269,9 +269,6 @@ "config/packages/stof_doctrine_extensions.yaml" ] }, - "swiftmailer/swiftmailer": { - "version": "v6.2.7" - }, "symfony/apache-pack": { "version": "1.0", "recipe": { @@ -582,20 +579,6 @@ "symfony/string": { "version": "v5.2.4" }, - "symfony/swiftmailer-bundle": { - "version": "2.5", - "recipe": { - "repo": "github.com/symfony/recipes", - "branch": "master", - "version": "2.5", - "ref": "ae4d22af30bbd484506bc1817c5a3ef72c855b93" - }, - "files": [ - "config/packages/dev/swiftmailer.yaml", - "config/packages/swiftmailer.yaml", - "config/packages/test/swiftmailer.yaml" - ] - }, "symfony/test-pack": { "version": "v1.0.7" },