1

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.

Kubitomakita
  • 63
  • 1
  • 7

1 Answers1

6

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.

over-engineer
  • 1,027
  • 1
  • 5
  • 15
Dinacel
  • 86
  • 2
  • 5
  • This looks very promising @Dinacel but I'm getting "Could not instantiate mail function" error. I have the sendmail_path correctly set and it's visible in phpinfo. When I ssh into WP container and try to run mhsendmail it's not showing anything. – Kubitomakita Apr 17 '17 at 10:46
  • Hi, I've fixed the Dockerfile because there were some errors : `usr/local/etc/php/conf.d/mailhog.ini` didn't had its starting slash (to ensure an absolute path definition, it was a typo) and `--smtp-addr=mailserver:1025` (it must be mailhog in your example, not mailserver as I've written) didn't had the correct hostname – Dinacel Apr 17 '17 at 17:18
  • Unfortunately it still doesn't work with mailhog host. I tried also localhost but no luck. No errors in the system logs, just PHPMailer saying "Could not instantiate mail function" – Kubitomakita Apr 20 '17 at 13:12
  • It works here without any other modifications. The wordpress dockerfile is successfully built ? Do you change the wordpress base configuration ? Is there third party plugins installed ? Can you send your phpinfo and the wordpress error log ? It's really strange. I'll check another time. – Dinacel Apr 21 '17 at 14:32
  • 1
    Which program is going to read `/usr/local/etc/php/conf.d/mailhog.ini`? I've got a [*similar question*](http://stackoverflow.com/questions/43888542/docker-find-sendmail-in-other-container). – Fabien Snauwaert May 10 '17 at 09:37