Skip to content

Commit

Permalink
fix: use new Brevo email sender instead of mailjet
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryde committed Jan 7, 2024
1 parent 3635aa9 commit ebc1636
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 33 deletions.
6 changes: 5 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ CORS_ALLOW_ORIGIN='^https?://(musicall.localhost|localhost|127\.0\.0\.1)(:[0-9]+
AWS_KEY=
AWS_SECRET=

OPEN_AI_KEY=
OPEN_AI_KEY=
###> symfony/brevo-mailer ###
# MAILER_DSN=brevo+api://KEY@default
# MAILER_DSN=brevo+smtp://USERNAME:PASSWORD@default
###< symfony/brevo-mailer ###
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 70 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/packages/mailer.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
framework:
mailer:
dsn: '%env(MAILER_DSN)%'

when@test:
framework:
mailer:
dsn: 'null://null'
30 changes: 30 additions & 0 deletions src/Service/Mail/Brevo/User/ConfirmRegistrationEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Service\Mail\Brevo\User;

use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;

class ConfirmRegistrationEmail
{
private const TEMPLATE_ID = '1';

public function __construct(private readonly MailerInterface $mailer)
{
}

public function send(string $recipientEmail, string $username, string $confirmEmail)
{
$email = (new Email())
->from('[email protected]')
->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);
}
}
27 changes: 0 additions & 27 deletions src/Service/Mail/RegistrationMail.php

This file was deleted.

10 changes: 6 additions & 4 deletions src/Service/User/ConfirmRegistrationSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
7 changes: 7 additions & 0 deletions tests/Api/User/RegisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function test_register()
$this->assertSame('super_username', $results[0]->getUsername());
$this->assertSame('[email protected]', $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()
Expand Down Expand Up @@ -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()
Expand All @@ -79,5 +85,6 @@ public function test_register_already_have_account()
$this->assertJsonEquals([
'errors' => 'you already have an account',
]);
$this->assertEmailCount(0);
}
}

0 comments on commit ebc1636

Please sign in to comment.