Bonjour,

J'essaie d'envoyer un mail via mailer symfony 5.3.10 et j'obtiens l'erreur " Connection to "localhost:1080" has been closed unexpectedly. "

mailcatcher est bien configuré car avec un script à part en local, cela fonctionne bien peu importe le port utilisé 'en changeant dans le php.ini et en démarrant sur le bon port mailcatcher)

mon mailer.yaml

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

ma commande

<?php

namespace App\Command;

use App\Repository\UserRepository;
use App\Repository\ContactRepository;
use App\Service\ContactService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;

Class SendContactCommand extends Command{
    private $contactRepository;
    private $userRepository;
    private $contactService;
    private $mailer;
    protected static $defaultName = 'app:send-contact';

    public function __construct
    (
        ContactRepository $contactRepository,
        UserRepository $userRepository,
        MailerInterface $mailer,
        ContactService $contactService
    )
    {
        $this->contactRepository = $contactRepository;
        $this->ContactService = $contactService;
        $this->userRepository = $userRepository;
        $this->mailer = $mailer;
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $toSend = $this->contactRepository->findBy(['isSend' => false]);
        $address = new Address($this->userRepository->getUserAuthorized()->getEmail(), $this->userRepository->getUserAuthorized()->getLastname() . '');

        foreach ($toSend as $mail){
            $email = (new Email())
            ->from($mail->getEmail())
            ->to($address)
            ->subject('Nouveau message de "' . $mail->getNom() .'"')
            ->text($mail->getMessage());

            $this->mailer->send($email);
            $this->contactService->isSend($mail);
        }
        return Command::SUCCESS;
    }

}

mon .env

# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
#  * .env                contains default values for the environment variables needed by the app
#  * .env.local          uncommitted file with local overrides
#  * .env.$APP_ENV       committed environment-specific defaults
#  * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=9a02bc46b67927a2e188a608813a84fb
###< symfony/framework-bundle ###

###> symfony/mailer ###
MAILER_DSN=smtp://localhost:1080
###< 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
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
DATABASE_URL="mysql://root:@127.0.0.1:3306/tba_blog?serverVersion=5.7"
# DATABASE_URL="postgresql://symfony:ChangeMe@127.0.0.1:5432/app?serverVersion=13&charset=utf8"
###< doctrine/doctrine-bundle ###

et si j'envoie via un autre port, il me dit "The "25" mailer DSN must contain a scheme."

Je vous remercie de votre aide

3 réponses


gillesr
Réponse acceptée

Bonjour,

le port 1080 est celui de l'interface web de mailcatcher.
Essai sur le port 1025 :


MAILER_DSN=smtp://localhost:1025
barpoi78
Auteur

Merci je n'avais pas compris et je pensais qu'il fallait laisser le port par défaut.

Oui, mais 1025 c'est le port pour l'envoi du serveur smtp (pour l'envoi des mails) et 1080 c'est celui pour l'interface http (qui te permet de les consulter dans ton navigateur).