Docker: Enabling mailing in php:apache (running WordPress)

Estimated reading time: 3 mins

Table of contents

Starting from

When using the php:apache image from https://hub.docker.com/_/php/ image mail support was not enabled. On my docker host postfix is already enabled and configured to accept and forward mails from my docker containers. So I should be able to send mails.

When I decided to install Wordpress by myself, I ended up with this Dockerfile and did config stuff using a volume over /var/www/html:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
FROM php:apache

# start here to see which apt and php packages are needed: https://github.com/docker-library/wordpress

RUN apt-get update && apt-get -y install libjpeg-dev libpng-dev \
  && docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
  && docker-php-ext-install gd mysqli opcache \
  && apt-get clean \

# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN { \
                echo 'opcache.memory_consumption=128'; \
                echo 'opcache.interned_strings_buffer=8'; \
                echo 'opcache.max_accelerated_files=4000'; \
                echo 'opcache.revalidate_freq=2'; \
                echo 'opcache.fast_shutdown=1'; \
                echo 'opcache.enable_cli=1'; \
        } > /usr/local/etc/php/conf.d/opcache-recommended.ini

# Apache Module aktivieren
RUN a2enmod rewrite expires

# Install WordPress by yourself

At the end I realized that mails are not working. Php’s answer when asking for the sendmail_path is this:

1
2
root@9f3b8b59bd67:/var/www/html# php -i | grep sendmail_path
sendmail_path =>  -t -i  =>  -t -i

And Php mail() result is this:

1
2
root@9f3b8b59bd67:/var/www/html# php -r "mail('test@some.where', 'Subject', 'Body');"
sh: 1: -t: not found

The manual approach first:

Entering the container:

1
docker exec -ti <contianer> bash

Then installing sSMTP

1
2
3
4
apt-get update
apt-get install ssmtp vim
#you need vim for editing the config file
vi /etc/ssmtp/ssmtp.conf

and configuring it. Here is my ssmtp.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids &lt; 1000
# Make this empty to disable rewriting.
root=youradmin-email@maildomain.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=172.18.0.1:25
# This is the hub in my docker swarm network

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=put.your.fqdn.hostname.here

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generat:wqed From: address
FromLineOverride=YES

Changing the sendmail_path for Php is easy as there is no php.ini by now:

1
echo "sendmail_path = /usr/sbin/ssmtp -t" &gt;&gt; /usr/local/etc/php/php.ini</pre>

After restarting the Apache

1
service apache2 graceful

Php is responding with a valid sendmail_path

1
2
root@9f3b8b59bd67:/var/www/html# php -i | grep sendmail_path
sendmail_path =&gt; /usr/sbin/ssmtp -t =&gt; /usr/sbin/ssmtp -t

Now Php mail() and also Wordpress mailing is working fine ;-).

The new Dockerfile:

You need to have the above ssmtp.conf file to build the image.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
FROM php:apache


# start here to see which apt and php packages are needed: https://github.com/docker-library/wordpress

RUN apt-get update && apt-get -y install ssmtp libjpeg-dev libpng-dev \
  && docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
  && docker-php-ext-install gd mysqli opcache \
  && apt-get clean \

# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN { \
                echo 'opcache.memory_consumption=128'; \
                echo 'opcache.interned_strings_buffer=8'; \
                echo 'opcache.max_accelerated_files=4000'; \
                echo 'opcache.revalidate_freq=2'; \
                echo 'opcache.fast_shutdown=1'; \
                echo 'opcache.enable_cli=1'; \
        } &gt; /usr/local/etc/php/conf.d/opcache-recommended.ini
# and adding a php.ini with the senmail path to ssmtp
RUN echo 'sendmail_path = /usr/sbin/ssmtp -t' &gt;&gt; /usr/local/etc/php/php.ini
# now copy the local ssmtp.conf file
COPY ssmtp.conf /etc/ssmtp/ssmtp.conf

# Apache Module aktivieren
RUN a2enmod rewrite expires

# Install WordPress by yourself
Posted on: Sun, 20 Aug 2017 16:07:09 +0200 by Markus Neuhold
  • Docker
  • Documentation
  • IBM i (AS/400) SysAdmin since 1997, Linux fanboy and loving open source, docker and all about tech and science.