Marek Buchtela
Marek Buchtela

Reputation: 1003

Symfony mail catcher (mailpit) refuses connection from code, works from console

I am trying to set up a development environment with Symfony running in Docker. Recently I installed the symfony/mailer package with the option to install a mailcatcher, which turned out to be axllent/mailpit.

However, when my code tries to send an email (I am just trying the registration form generated by symfony with user email verification), I get the following error:

    Connection could not be established with host "127.0.0.1:1025": stream_socket_client(): Unable to connect to 127.0.0.1:1025 (Connection refused)

However, if I try in console, using

    symfony console mailer:test [email protected]

or

    mailpit sendmail -t -S 127.0.0.1:1025 < email.txt

everything works fine and the email appears in the mailpit web UI (which I can access just fine on localhost:8025).

Here is the mailpit part of my compose.yaml:

    mailer:
      image: axllent/mailpit
      restart: unless-stopped
      volumes:
        - ./mailer:/data
      ports:
        - 1025:1025
        - 8025:8025
      environment:
        MP_SMTP_AUTH_ACCEPT_ANY: 1
        MP_SMTP_AUTH_ALLOW_INSECURE: 1
        MP_MAX_MESSAGES: 5000
        MP_DATABASE: /data/mailpit.db

And in .env, I have the following DSN:

    MAILER_DSN=smtp://127.0.0.1:1025

How do I make the code connect/send emails properly too?

If at all relevant, here is the full Stack Trace: Exception stack trace

Upvotes: 0

Views: 1965

Answers (1)

daftQ1
daftQ1

Reputation: 41

You need to use the Docker service name (mailer) as the hostname instead of your localhost (127.0.0.1) inside your container running Symfony. Put this in your .env.dev or .env.dev.local:

    MAILER_DSN=smtp://mailer:1025

Then check which ports are exposed by mailer service with docker compose ps. It should return something like this:

NAME                 ... PORTS
my-project-mailer-1  ... 1110/tcp, 0.0.0.0:35433->1025/tcp, 0.0.0.0:44957->8025/tcp

Note 0.0.0.0:44957->8025. In this case the web interface of the mailer service is available at http://localhost:44957.

Or use fixed ports in your Docker compose to make the web interface available only on your local computer at port 48025 (http://localhost:48025).

mailer:
      image: axllent/mailpit
      # ...
      ports:
        - "1025:1025"
        - "127.0.0.1:48025:8025"
      # ...

Upvotes: 4

Related Questions