From ebc16361e3a3a208569d4457221ec74cf5f17e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my?= Date: Sun, 7 Jan 2024 16:48:42 +0100 Subject: [PATCH] fix: use new Brevo email sender instead of mailjet --- .env | 6 +- composer.json | 1 + composer.lock | 71 ++++++++++++++++++- config/packages/mailer.yaml | 5 ++ .../Brevo/User/ConfirmRegistrationEmail.php | 30 ++++++++ src/Service/Mail/RegistrationMail.php | 27 ------- .../User/ConfirmRegistrationSender.php | 10 +-- symfony.lock | 9 +++ tests/Api/User/RegisterTest.php | 7 ++ 9 files changed, 133 insertions(+), 33 deletions(-) create mode 100644 src/Service/Mail/Brevo/User/ConfirmRegistrationEmail.php delete mode 100644 src/Service/Mail/RegistrationMail.php diff --git a/.env b/.env index 6e15f91f..98c7802d 100644 --- a/.env +++ b/.env @@ -59,4 +59,8 @@ CORS_ALLOW_ORIGIN='^https?://(musicall.localhost|localhost|127\.0\.0\.1)(:[0-9]+ AWS_KEY= AWS_SECRET= -OPEN_AI_KEY= \ No newline at end of file +OPEN_AI_KEY= +###> symfony/brevo-mailer ### +# MAILER_DSN=brevo+api://KEY@default +# MAILER_DSN=brevo+smtp://USERNAME:PASSWORD@default +###< symfony/brevo-mailer ### diff --git a/composer.json b/composer.json index 9a9a90ef..25616b82 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "sentry/sentry": "^3.22", "sentry/sentry-symfony": "^4.12", "symfony/asset": "^6.1", + "symfony/brevo-mailer": "^6.0", "symfony/console": "^6.1.4", "symfony/dotenv": "^6.1", "symfony/expression-language": "^6.1.3", diff --git a/composer.lock b/composer.lock index dc7746d8..93e27068 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5b511159268494118d874a4d9ddb9de0", + "content-hash": "10a5ec2ca09b52aabc1efde15686b179", "packages": [ { "name": "api-platform/core", @@ -6458,6 +6458,75 @@ ], "time": "2023-10-31T08:40:20+00:00" }, + { + "name": "symfony/brevo-mailer", + "version": "v6.4.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/brevo-mailer.git", + "reference": "83db87e0f44653cd40aeef54a2f57ab6bfccadfe" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/brevo-mailer/zipball/83db87e0f44653cd40aeef54a2f57ab6bfccadfe", + "reference": "83db87e0f44653cd40aeef54a2f57ab6bfccadfe", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/mailer": "^5.4.21|^6.2.7|^7.0" + }, + "conflict": { + "symfony/mime": "<6.2" + }, + "require-dev": { + "symfony/http-client": "^6.3|^7.0", + "symfony/webhook": "^6.3|^7.0" + }, + "type": "symfony-mailer-bridge", + "autoload": { + "psr-4": { + "Symfony\\Component\\Mailer\\Bridge\\Brevo\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Pierre Tanguy", + "homepage": "https://github.com/petanguy" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Brevo Mailer Bridge", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/brevo-mailer/tree/v7.0.0-RC1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2023-11-06T17:20:05+00:00" + }, { "name": "symfony/cache", "version": "v6.4.0", diff --git a/config/packages/mailer.yaml b/config/packages/mailer.yaml index 56a650d8..696b7fdf 100644 --- a/config/packages/mailer.yaml +++ b/config/packages/mailer.yaml @@ -1,3 +1,8 @@ framework: mailer: dsn: '%env(MAILER_DSN)%' + +when@test: + framework: + mailer: + dsn: 'null://null' \ No newline at end of file diff --git a/src/Service/Mail/Brevo/User/ConfirmRegistrationEmail.php b/src/Service/Mail/Brevo/User/ConfirmRegistrationEmail.php new file mode 100644 index 00000000..325d4613 --- /dev/null +++ b/src/Service/Mail/Brevo/User/ConfirmRegistrationEmail.php @@ -0,0 +1,30 @@ +from('no-reply@musicall.com') + ->to($recipientEmail) + ->text('Confirmer votre email'); + $email->getHeaders() + ->addTextHeader('templateId', self::TEMPLATE_ID) + ->addParameterizedHeader('params', 'params', [ + 'confirmation_link' => $confirmEmail, + 'username' => $username, + ]); + $this->mailer->send($email); + } +} \ No newline at end of file diff --git a/src/Service/Mail/RegistrationMail.php b/src/Service/Mail/RegistrationMail.php deleted file mode 100644 index 62a71932..00000000 --- a/src/Service/Mail/RegistrationMail.php +++ /dev/null @@ -1,27 +0,0 @@ -mailer->send( - $this->arrayMailBuilder->build( - $recipientEmail, - self::TEMPLATE_ID, - "Confirmer votre email", - [ - 'username' => $username, - 'confirmation_link' => $confirmEmail, - ] - ) - ); - } -} diff --git a/src/Service/User/ConfirmRegistrationSender.php b/src/Service/User/ConfirmRegistrationSender.php index 9413a059..de0084bd 100644 --- a/src/Service/User/ConfirmRegistrationSender.php +++ b/src/Service/User/ConfirmRegistrationSender.php @@ -3,19 +3,21 @@ namespace App\Service\User; use App\Entity\User; -use App\Service\Mail\RegistrationMail; +use App\Service\Mail\Brevo\User\ConfirmRegistrationEmail; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; class ConfirmRegistrationSender { - public function __construct(private readonly RouterInterface $router, private readonly RegistrationMail $registrationMail) - { + public function __construct( + private readonly RouterInterface $router, + private readonly ConfirmRegistrationEmail $confirmRegistrationEmail + ) { } public function sendConfirmationEmail(User $user): void { $route = $this->router->generate('app_register_confirm', ['token' => $user->getToken()], UrlGeneratorInterface::ABSOLUTE_URL); - $this->registrationMail->send($user->getEmail(), $user->getUsername(), $route); + $this->confirmRegistrationEmail->send($user->getEmail(), $user->getUsername(), $route); } } diff --git a/symfony.lock b/symfony.lock index cbfda6ff..f9e8c1f4 100644 --- a/symfony.lock +++ b/symfony.lock @@ -478,6 +478,15 @@ "symfony/asset": { "version": "v4.2.0" }, + "symfony/brevo-mailer": { + "version": "6.4", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "main", + "version": "6.4", + "ref": "7c1038ceb733175f610a913a885a7c8b552b703a" + } + }, "symfony/browser-kit": { "version": "v5.4.2" }, diff --git a/tests/Api/User/RegisterTest.php b/tests/Api/User/RegisterTest.php index d526f68f..9aa9f7c8 100644 --- a/tests/Api/User/RegisterTest.php +++ b/tests/Api/User/RegisterTest.php @@ -38,6 +38,11 @@ public function test_register() $this->assertSame('super_username', $results[0]->getUsername()); $this->assertSame('super_email@mail.com', $results[0]->getEmail()); $this->assertNotSame('password', $results[0]->getPassword()); // we assert that we don't record plain text password in db + $this->assertEmailCount(1); + + $email = $this->getMailerMessage(); + + $this->assertEmailTextBodyContains($email, 'Confirmer votre email'); } public function test_register_with_errors() @@ -67,6 +72,7 @@ public function test_register_with_errors() 'code' => '9ff3fdc4-b214-49db-8718-39c315e33d45', ], ]); + $this->assertEmailCount(0); } public function test_register_already_have_account() @@ -79,5 +85,6 @@ public function test_register_already_have_account() $this->assertJsonEquals([ 'errors' => 'you already have an account', ]); + $this->assertEmailCount(0); } } \ No newline at end of file