dockersymfonysmtpswiftmailermailhog

Why connexion could not be established with host on Symfony and Mailhog on Docker


I have this exception went i sent mail through Symfony SwiftMailer:

Exception occurred while flushing email queue: Connection could not be established with host 127.0.0.1 :stream_socket_client(): unable to connect to 127.0.0.1:1025 (Connection refused)

I can access to Mailhog interface. The container is up and ports seems open and ready.

See below my docker-composer for Mailhog:

version: '3'
services:
    mailhog:
        restart: always
        image: mailhog/mailhog:latest
        container_name: mailhog
        ports:
            - 1025:1025
            - 8025:8025
        volumes:
            - /var/run/docker.sock:/tmp/docker.sock:ro

And my Swift mailer env configuration:

MAILER_TRANSPORT=smtp
MAILER_HOST=127.0.0.1
MAILER_PORT=1025
MAILER_USER=null
MAILER_PASSWORD=null

Why i can't access to Mailhog SMTP ?

I think it comes from Docker but how can I be sure? What did I miss?


Solution

  • To resolve the problem:

    Instead of 127.0.0.1 => MAILER_HOST=mailhog like Artem said.

    And add:

    external_links:
                - mailhog
    

    To your container in docker-compose.yml file of all projects.