phpdockermailcatcher

Use PHP mail() function in docker installation with mailcatcher


I have set up a local development environment using docker compose. It consists of nginx, php-fpm, mariadb, and mailcatcher. Everything is working good, but I can't make the mail() function work.

This is my compose.yaml

version: '3.8'

services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./html:/var/www/html
    networks:
      - internal
    depends_on:
      - php    

  php:
    build:
      context: .
      dockerfile: ./php/Dockerfile
    volumes:
      - ./html:/var/www/html
      - ./custom-php.ini:/usr/local/etc/php/conf.d/custom-php.ini
    environment:
      PHP_INI_SCAN_DIR: /usr/local/etc/php/conf.d      
    networks:
      - internal    

  mariadb:
    image: mariadb:10.4    
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: dbname
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: dbpassword
    ports:
      - "3306:3306"
    volumes:
      - mariadb_jazdy_data:/var/lib/mysql
    networks:
      - internal

  mailcatcher:
    image: sj26/mailcatcher
    ports:
      - "1080:1080"
      - "1025:1025"    

networks:
  internal:

volumes:
  mariadb_jazdy_data:

This is my Dockerfile

FROM php:7.4-fpm
RUN curl -sSLf \
        -o /usr/local/bin/install-php-extensions \
        https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions && \
    chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions mysqli

and this is my custom-php.ini

; Enable mysqli extension
extension=mysqli.so

[mail function]
SMTP=127.0.0.1
smtp_port=1025
sendmail_from=sender@example.com

I tried different configuration for the SMTP, including localhost, or mailcatcher. Without any success.

What am I missing here? I really would like to make this work as I literally spent hours of googling and asking chat gpt. Thanks.

I tried using different docker images, installing sendmail in Dockerfile (https://r.je/sendmail-php-docker), but nothing seems to be working.


Solution

  • I'm gonna answer my own question with the correct solution where Alex Howansky led me to.

    However, I've made some changes also to the original compose.yaml, where I forgot to add the network directive under mailcatcher. But later, I found out, it didn't have to be there, at all.

    So these is the final configuration how I made it work.

    compose.yaml

    version: '3.8'
    
    services:
      nginx:
        image: nginx:latest
        ports:
          - "80:80"
        volumes:
          - ./nginx.conf:/etc/nginx/nginx.conf
          - ./html:/var/www/html    
        depends_on:
          - php    
    
      php:
        build:
          context: .
          dockerfile: ./php/Dockerfile
        volumes:
          - ./html:/var/www/html
          - ./custom-php.ini:/usr/local/etc/php/conf.d/custom-php.ini
        environment:
          PHP_INI_SCAN_DIR: /usr/local/etc/php/conf.d          
    
      mariadb:
        image: mariadb:10.4    
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: dbname
          MYSQL_USER: dbuser
          MYSQL_PASSWORD: dbpassword
        ports:
          - "3306:3306"
        volumes:
          - mariadb_jazdy_data:/var/lib/mysql    
    
      mailcatcher:
        image: sj26/mailcatcher
        ports:
          - "1080:1080"          
    
    volumes:
      mariadb_jazdy_data:
    

    Dockerfile

    FROM php:7.4-fpm
    RUN apt-get update && apt-get install -y build-essential software-properties-common
    RUN apt-get install -y libsqlite3-dev ruby-dev
    RUN gem install mailcatcher
    RUN curl -sSLf \
            -o /usr/local/bin/install-php-extensions \
            https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions && \
        chmod +x /usr/local/bin/install-php-extensions && \
        install-php-extensions mysqli
    RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
    

    custom-ini.php

    ; Enable mysqli extension
    extension=mysqli.so
    
    [mail function]
    sendmail_path=/usr/bin/env catchmail --smtp-ip mailcatcher --smtp-port 1025