remove swiftmailer and add symfony/mailer

This commit is contained in:
Simon Vieille 2022-01-29 22:15:53 +01:00
parent 5c5e114eb7
commit 990f18c423
9 changed files with 33 additions and 165 deletions

13
.env
View file

@ -15,9 +15,13 @@
###> symfony/framework-bundle ### ###> symfony/framework-bundle ###
APP_ENV=dev APP_ENV=dev
APP_SECRET=e6e287f176fe2c69112fc620e1801bf0 APP_SECRET=
###< symfony/framework-bundle ### ###< symfony/framework-bundle ###
###> symfony/mailer ###
MAILER_DSN=smtp://localhost
###< symfony/mailer ###
###> doctrine/doctrine-bundle ### ###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # 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 # 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="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" # DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8"
###< doctrine/doctrine-bundle ### ###< 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 ###

View file

@ -50,7 +50,6 @@
"symfony/security-bundle": "5.4.*", "symfony/security-bundle": "5.4.*",
"symfony/serializer": "5.4.*", "symfony/serializer": "5.4.*",
"symfony/string": "5.4.*", "symfony/string": "5.4.*",
"symfony/swiftmailer-bundle": "^3.5",
"symfony/translation": "5.4.*", "symfony/translation": "5.4.*",
"symfony/twig-bundle": "^5.2", "symfony/twig-bundle": "^5.2",
"symfony/validator": "5.4.*", "symfony/validator": "5.4.*",

View file

@ -13,7 +13,6 @@ return [
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true], Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true], Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true], Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true],
Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
Scheb\TwoFactorBundle\SchebTwoFactorBundle::class => ['all' => true], Scheb\TwoFactorBundle\SchebTwoFactorBundle::class => ['all' => true],
Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle::class => ['all' => true], Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle::class => ['all' => true],
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true], Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true],

View file

@ -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']

View file

@ -1,3 +1,5 @@
framework: framework:
mailer: mailer:
dsn: '%env(MAILER_DSN)%' dsn: '%env(MAILER_DSN)%'
headers:
From: 'Example <example@localhost>'

View file

@ -1,4 +0,0 @@
swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
sender_address: system@localhost

View file

@ -1,2 +0,0 @@
swiftmailer:
disable_delivery: true

View file

@ -2,9 +2,8 @@
namespace App\Core\Notification; namespace App\Core\Notification;
use Swift_Attachment; use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Swift_Mailer; use Symfony\Component\Mailer\MailerInterface;
use Swift_Message;
use Twig\Environment as TwigEnvironment; use Twig\Environment as TwigEnvironment;
/** /**
@ -14,56 +13,20 @@ use Twig\Environment as TwigEnvironment;
*/ */
class MailNotifier class MailNotifier
{ {
/** protected MailerInterface $mailer;
* @var Swift_Mailer protected array $attachments = [];
*/ protected array $recipients = [];
protected $mailer; protected array $bccRecipients = [];
protected ?string $subject = null;
protected ?string $from = null;
protected ?string $replyTo = null;
/** public function __construct(TwigEnvironment $twig, MailerInterface $mailer)
* @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)
{ {
$this->mailer = $mailer; $this->mailer = $mailer;
$this->twig = $twig; $this->twig = $twig;
} }
/**
* @return EmailNotifier
*/
public function setMailer(Swift_Mailer $mailer): self public function setMailer(Swift_Mailer $mailer): self
{ {
$this->mailer = $mailer; $this->mailer = $mailer;
@ -76,9 +39,6 @@ class MailNotifier
return $this->mailer; return $this->mailer;
} }
/**
* @return EmailNotifier
*/
public function setRecipients(array $recipients): self public function setRecipients(array $recipients): self
{ {
$this->recipients = $recipients; $this->recipients = $recipients;
@ -91,9 +51,6 @@ class MailNotifier
return $this->recipients; return $this->recipients;
} }
/**
* @return EmailNotifier
*/
public function setBccRecipients(array $bccRecipients): self public function setBccRecipients(array $bccRecipients): self
{ {
$this->bccRecipients = $bccRecipients; $this->bccRecipients = $bccRecipients;
@ -106,11 +63,6 @@ class MailNotifier
return $this->bccRecipients; return $this->bccRecipients;
} }
/**
* @param string $subject
*
* @return EmailNotifier
*/
public function setSubject(?string $subject): self public function setSubject(?string $subject): self
{ {
$this->subject = $subject; $this->subject = $subject;
@ -123,11 +75,6 @@ class MailNotifier
return $this->subject; return $this->subject;
} }
/**
* @param mixed $from
*
* @return EmailNotifier
*/
public function setFrom($from): self public function setFrom($from): self
{ {
$this->from = $from; $this->from = $from;
@ -135,21 +82,11 @@ class MailNotifier
return $this; return $this;
} }
/**
* @return mixed
*/
public function getFrom(): ?string public function getFrom(): ?string
{ {
return $this->from; return $this->from;
} }
/**
* Set the value of "replyTo".
*
* @param string $replyTo
*
* @return EmailNotifier
*/
public function setReplyTo($replyTo): self public function setReplyTo($replyTo): self
{ {
$this->replyTo = $replyTo; $this->replyTo = $replyTo;
@ -157,19 +94,11 @@ class MailNotifier
return $this; return $this;
} }
/*
* Get the value of "replyTo".
*
* @return string
*/
public function getReplyTo(): ?string public function getReplyTo(): ?string
{ {
return $this->replyTo; return $this->replyTo;
} }
/**
* @return EmailNotifier
*/
public function setAttachments(array $attachments): self public function setAttachments(array $attachments): self
{ {
$this->attachments = $attachments; $this->attachments = $attachments;
@ -182,9 +111,6 @@ class MailNotifier
return $this->attachments; return $this->attachments;
} }
/**
* @return EmailNotifier
*/
public function addRecipient(string $email, bool $isBcc = false): self public function addRecipient(string $email, bool $isBcc = false): self
{ {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
@ -204,9 +130,6 @@ class MailNotifier
return $this; return $this;
} }
/**
* @return EmailNotifier
*/
public function addRecipients(array $emails, bool $isBcc = false): self public function addRecipients(array $emails, bool $isBcc = false): self
{ {
foreach ($emails as $email) { foreach ($emails as $email) {
@ -216,19 +139,11 @@ class MailNotifier
return $this; return $this;
} }
/**
* @return EmailNotifier
*/
public function addRecipientByAccount(Account $account, bool $isBcc = false): self public function addRecipientByAccount(Account $account, bool $isBcc = false): self
{ {
return $this->addRecipient($account->getEmail(), $isBcc); return $this->addRecipient($account->getEmail(), $isBcc);
} }
/**
* @param mixed $accounts
*
* @return EmailNotifier
*/
public function addRecipientsByAccounts($accounts, bool $isBcc = false) public function addRecipientsByAccounts($accounts, bool $isBcc = false)
{ {
if (!is_array($accounts)) { if (!is_array($accounts)) {
@ -242,9 +157,6 @@ class MailNotifier
return $this; return $this;
} }
/**
* @return EmailNotifier
*/
public function addAttachment(string $attachment): self public function addAttachment(string $attachment): self
{ {
if (!in_array($attachment, $this->attachments)) { if (!in_array($attachment, $this->attachments)) {
@ -254,9 +166,6 @@ class MailNotifier
return $this; return $this;
} }
/**
* @return EmailNotifier
*/
public function addAttachments(array $attachments): self public function addAttachments(array $attachments): self
{ {
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
@ -266,9 +175,6 @@ class MailNotifier
return $this; return $this;
} }
/**
* @return EmailNotifier
*/
public function init(): self public function init(): self
{ {
$this $this
@ -281,58 +187,50 @@ class MailNotifier
return $this; return $this;
} }
/**
* @return EmailNotifier
*/
public function notify(string $template, array $data = [], string $type = 'text/html'): self public function notify(string $template, array $data = [], string $type = 'text/html'): self
{ {
$message = $this->createMessage( $message = $this->createMessage();
$this->twig->render( $message->context($data);
$template,
$data if (in_array($type, ['text/plain', 'text'])) {
), $message->textTemplate($template);
$type } else {
); $message->htmlTemplate($template);
}
$this->mailer->send($message); $this->mailer->send($message);
return $this; 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()) { if ($this->getSubject()) {
$message->setSubject($this->getSubject()); $message->subject($this->getSubject());
} }
if ($this->getFrom()) { if ($this->getFrom()) {
$message->setFrom($this->getFrom()); $message->from($this->getFrom());
} }
if ($this->getReplyTo()) { if ($this->getReplyTo()) {
$message->setReplyTo($this->getReplyTo()); $message->replyTo($this->getReplyTo());
} }
if (count($this->getRecipients()) > 0) { if (count($this->getRecipients()) > 0) {
$message->setTo($this->getRecipients()); $message->to(...$this->getRecipients());
} }
if (count($this->getBccRecipients()) > 0) { if (count($this->getBccRecipients()) > 0) {
$message->setBcc($this->getBccRecipients()); $message->bcc(...$this->getBccRecipients());
} }
foreach ($this->getAttachments() as $attachment) { foreach ($this->getAttachments() as $attachment) {
if (is_object($attachment) && $attachment instanceof Swift_Attachment) { $message->attachFromPath($attachment);
$message->attach($attachment);
} elseif (is_string($attachment) && file_exists($attachment) && is_readable($attachment) && !is_dir($attachment)) {
$message->attach(Swift_Attachment::fromPath($attachment));
}
} }
$message->setBody($body, $type);
return $message; return $message;
} }
} }

View file

@ -269,9 +269,6 @@
"config/packages/stof_doctrine_extensions.yaml" "config/packages/stof_doctrine_extensions.yaml"
] ]
}, },
"swiftmailer/swiftmailer": {
"version": "v6.2.7"
},
"symfony/apache-pack": { "symfony/apache-pack": {
"version": "1.0", "version": "1.0",
"recipe": { "recipe": {
@ -582,20 +579,6 @@
"symfony/string": { "symfony/string": {
"version": "v5.2.4" "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": { "symfony/test-pack": {
"version": "v1.0.7" "version": "v1.0.7"
}, },