Setting Mailpit to work with Laravel and Docker

950 Views Asked by At

I want to test email using mailpit but could not get in to work. I keep getting this error:

Connection could not be established with host mailpit :stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution

I have mailpit running and can access the web interface. This is my env setup.

MAIL_MAILER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"
MAIL_ENCRYPTION=null

I have also tried the following settings:

MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"
MAIL_ENCRYPTION=null

I try to change the host to 0.0.0.0 , localhost,127.0.0.1 and mailpit but it didn't work.

Here is my docker-compose.yml file code :

version: "3.9"

services:
  mailpit:
    image: axllent/mailpit
    container_name: mailpit
    restart: always
    hostname: localhost
    volumes:
      - ./data:/data
    ports:
      - 8025:8025
      - 1025:1025
    environment:
      MP_MAX_MESSAGES: 5000
      MP_DATA_FILE: /data/mailpit.db
      MP_SMTP_AUTH_ACCEPT_ANY: 1
      MP_SMTP_AUTH_ALLOW_INSECURE: 1

And I have also created a Docker file in my mailtrap directory my file code is :

RUN -d \
--name=mailpit \
--restart unless-stopped \
-p 8025:8025 \
-p 1025:1025 \
axllent/mailpit

I tried mailtrap and it's working great but I still would love to be able to do this locally. Thank you

1

There are 1 best solutions below

3
Second2None On

You need to make sure you add it to the correct network inside your docker-compose.yml

If you are using laravel sail it should look like this:

  mailpit:
    image: 'axllent/mailpit:latest'
    ports:
      - '${FORWARD_MAILPIT_PORT:-1025}:1025'
      - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025'
    networks:
      - sail

If you aren't then you should have a networks section in your docker-compose.yml file that looks similar to this:

networks:
  sail:
    driver: bridge

Where sail is the name of the network, then just add the network accordingly to your mail pit service. It should also be attached to the laravel service.