wordpressdockerdocker-composemailcatcher

Docker connect Mail catcher with WordPress


I want to set a sendmail_path in WordPress' container and use a sendmail provided by another container. In my case its MailHog.

So this is my docker-compose:

version: '2'
services:
    wordpress:
        image: wordpress
        links:
            - db:mysql
            - mailhog
        ports:
            - 80:80
        domainname: foo.com
        hostname: foo
        volumes:
            - ./public:/var/www/html
        environment:
            WORDPRESS_DB_PASSWORD: example
        depends_on:
           - mailhog

    mailhog:
        image: mailhog/mailhog
        ports:
            - 1025:1025
            - 8025:8025

    db:
        image: mariadb
        environment:
            MYSQL_ROOT_PASSWORD: example

I tried executing the command: "echo 'sendmail_path = \"/usr/local/bin/mailhog sendmail\"' > /usr/local/etc/php/conf.d/mail.ini" on WordPress container but it actually prints it...

Does these two have to share the volumes?

PS. I know I can use it as a SMTP server in the APP but I want to deal with it in more automated way.


Solution

  • You don't have MailHog installed in the WordPress container, so the path /usr/local/bin/mailhog doesn't exist.

    What you want to do is to send emails via sendmail and those emails must be caught by MailHog. To do this, you must extend the WordPress Dockerfile:

    FROM wordpress
    RUN curl --location --output /usr/local/bin/mhsendmail https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 && \
        chmod +x /usr/local/bin/mhsendmail
    
    RUN echo 'sendmail_path="/usr/local/bin/mhsendmail --smtp-addr=mailhog:1025 --from=no-reply@docker.dev"' > /usr/local/etc/php/conf.d/mailhog.ini
    

    Note the --smtp-addr parameter must be in the form <mailhog_hostname>:<mailhog_port>.

    Change your docker-compose.yml to build your Dockerfile.

    version: '2'
    services:
        wordpress:
            build:
                context: ./
                dockerfile: ./Dockerfile
            links:
                - db:mysql
                - mailhog
            ports:
                - 80:80
            domainname: foo.com
            hostname: foo
            volumes:
                - ./public:/var/www/html
            environment:
                WORDPRESS_DB_PASSWORD: example
            depends_on:
               - mailhog
        
        mailhog:
            image: mailhog/mailhog
            ports:
                - 1025:1025
                - 8025:8025
        
        db:
            image: mariadb
            environment:
                MYSQL_ROOT_PASSWORD: example
    

    In this example, the Dockerfile you have written must be named "Dockerfile" and must be in the current directory (where you run docker-compose). You can change the path accordingly. You can remove the 1025:1025 ports entry if you don't need to connect to it from the host.

    Now the function mail() should work as intended.