Skip to content

Commit

Permalink
tec: Setup a default Mailbox during seeding
Browse files Browse the repository at this point in the history
I also changed the default SMTP address so it matches with the Mailbox
address.
This allows to have the same mailbox to send and receive emails. It will
facilitate the process of creating/answering to tickets by email.
  • Loading branch information
marien-probesys committed Jun 22, 2023
1 parent a659c8e commit 1be563f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ DATABASE_URL="postgresql://postgres:postgres@database:5432/bileto?serverVersion=
# Configure your mail server. It is used to send email notifications to the users.
# Uncomment both MAILER_* lines and set them to your needs.
# More documentation at https://symfony.com/doc/7.0/mailer.html (“Transport Setup”).
MAILER_DSN=smtp://noreply%40example.com:secret@mailserver:3025
MAILER_FROM=noreply@example.com
MAILER_DSN=smtp://support%40example.com:secret@mailserver:3025
MAILER_FROM=support@example.com
11 changes: 9 additions & 2 deletions docs/developers/greenmail.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ Only SMTP and IMAP protocols are served as we only need these at the moment.

SMTP is already configured in Bileto to use the GreenMail server.
You can find the configuration in the file [`.env`](/.env).
It uses the address `noreply@example.com` to send emails.
It uses the address `support@example.com` to send emails.

You can configure a mailbox with IMAP in the interface.
When you setup the database, it also configures an IMAP Mailbox with the same address.
If it’s not, run:

```console
$ ./docker/bin/console db:seeds:load
```

You can configure more mailboxes with IMAP in the interface.
As an admin, go to the “Settings > Mailboxes” and create a new mailbox.
Configure it with the following information:

Expand Down
2 changes: 1 addition & 1 deletion env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ APP_SECRET=change-me
# Uncomment both MAILER_* lines and set them to your needs.
# More documentation at https://symfony.com/doc/7.0/mailer.html (“Transport Setup”).
# MAILER_DSN=smtp://user:pass@mail.example.com:465
# MAILER_FROM=noreply@example.com
# MAILER_FROM=support@example.com
67 changes: 27 additions & 40 deletions src/Command/SeedsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
namespace App\Command;

use App\Repository\AuthorizationRepository;
use App\Repository\MailboxRepository;
use App\Repository\MessageRepository;
use App\Repository\OrganizationRepository;
use App\Repository\RoleRepository;
use App\Repository\TicketRepository;
use App\Repository\UserRepository;
use App\Security\Encryptor;
use App\Utils\Random;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -27,48 +29,19 @@
)]
class SeedsCommand extends Command
{
private string $environment;

private EntityManagerInterface $entityManager;

private AuthorizationRepository $authorizationRepository;

private MessageRepository $messageRepository;

private OrganizationRepository $orgaRepository;

private RoleRepository $roleRepository;

private TicketRepository $ticketRepository;

private UserRepository $userRepository;

private UserPasswordHasherInterface $passwordHasher;

public function __construct(
string $environment,
EntityManagerInterface $entityManager,
AuthorizationRepository $authorizationRepository,
MessageRepository $messageRepository,
OrganizationRepository $orgaRepository,
RoleRepository $roleRepository,
TicketRepository $ticketRepository,
UserRepository $userRepository,
UserPasswordHasherInterface $passwordHasher,
private string $environment,
private EntityManagerInterface $entityManager,
private AuthorizationRepository $authorizationRepository,
private MailboxRepository $mailboxRepository,
private MessageRepository $messageRepository,
private OrganizationRepository $orgaRepository,
private RoleRepository $roleRepository,
private TicketRepository $ticketRepository,
private UserRepository $userRepository,
private Encryptor $encryptor,
private UserPasswordHasherInterface $passwordHasher,
) {
$this->environment = $environment;

$this->entityManager = $entityManager;

$this->authorizationRepository = $authorizationRepository;
$this->messageRepository = $messageRepository;
$this->orgaRepository = $orgaRepository;
$this->roleRepository = $roleRepository;
$this->ticketRepository = $ticketRepository;
$this->userRepository = $userRepository;

$this->passwordHasher = $passwordHasher;

parent::__construct();
}

Expand Down Expand Up @@ -185,6 +158,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->authorizationRepository->grant($userCharlie, $roleUser, $orgaFriendlyCoorp);
}

// Seed mailboxes
$this->mailboxRepository->findOneOrCreateBy([
'name' => 'support@example.com',
], [
'host' => 'mailserver',
'protocol' => 'imap',
'port' => 3143,
'encryption' => 'none',
'username' => 'support@example.com',
'password' => $this->encryptor->encrypt('secret'),
'authentication' => 'normal',
'folder' => 'INBOX',
]);

// Seed tickets
$ticketEmails = $this->ticketRepository->findOneOrCreateBy([
'title' => 'My emails are not received',
Expand Down
3 changes: 3 additions & 0 deletions src/Repository/MailboxRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
* @method Mailbox|null findOneBy(array $criteria, array $orderBy = null)
* @method Mailbox[] findAll()
* @method Mailbox[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*
* @method Mailbox findOneOrCreateBy(array $criteria, array $valuesToCreate = [], bool $flush = false)
*/
class MailboxRepository extends ServiceEntityRepository implements UidGeneratorInterface
{
use UidGeneratorTrait;
use FindOrCreateTrait;

public function __construct(ManagerRegistry $registry)
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Command/SeedsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace App\Tests\Command;

use App\Tests\CommandTestsHelper;
use App\Tests\Factory\MailboxFactory;
use App\Tests\Factory\OrganizationFactory;
use App\Tests\Factory\RoleFactory;
use App\Tests\Factory\UserFactory;
Expand All @@ -29,5 +30,6 @@ public function testExecute(): void
$this->assertSame(3, RoleFactory::count());
$this->assertSame(4, OrganizationFactory::count());
$this->assertSame(3, UserFactory::count());
$this->assertSame(1, MailboxFactory::count());
}
}

0 comments on commit 1be563f

Please sign in to comment.