Dockerizing everything is fun and gives rise to sooo many ideas and opportunities. However, sometimes it’s also annoying as …. For example, I just tried to use a Docker container for a PHP application that sends emails. Usually, if your server is configured ok-ish, it works out of the box and I never had problems with something like that.

## The Issue

In times of Docker there is just one application per container. That means the PHP container doesn’t know anything about emailing. Even worse, the configuration tool that comes with PHP tries configuring the sendmail_path to something like $SENDMAILBINARY -t -i. That obviously fails, because there is no sendmail binary and $SENDMAILBINARY remains empty, thus the actual setting becomes:

That, in turn, leads to absurd messages in your log files, because there is no such binary as -t:

WARNING: [pool www] child 7 said into stderr: "sh: 1: -t: not found"


Hard times to start debugging that issue..

## The Solution

To solve this problem I forked the php:fpm image to install sSMTP, a very simple MTA that is able to deliver mail to a mail hub. Afterwards I needed to configure the sSMTP as well as the PHP mail setup.

### Install sSMTP into php:fpm

Nothing easier than that, just create a Dockerfile based on php:fpm and install sSMTP through apt:

Docker-build that image either through command line or using Docker Compose or whatever is your workflow. For this example, let’s call this image binfalse/php-fpm-extended.

### Setup for the sSMTP

PLEASE NOTE: sSMTP is not maintained anymore! Please switch to msmtp, for example, as I explained in Migrating from sSMTP to msmtp.

Configuring the sSMTP is easy. Basically, all you need to do is to specify the address to the mail hub using the mailhub option. However, as my mail server is running on a different physical server I also want to enable encryption, so I set UseTLS and UseSTARTTLS to YES. Docker containers usually get cryptic names, so I reset the hostname using the hostname variable. And last but not least I allowed the applications to overwrite of the From field in emails using the FromLineOverride. Finally, your full configuration may look like:

Just store that in a file, e.g. /path/to/ssmtp.conf. We’ll mount that into the container later on.

### Configure mail for php:fpm

PLEASE NOTE: sSMTP is not maintained anymore! Please switch to msmtp, for example, as I explained in Migrating from sSMTP to msmtp.

Even if we installed the sSMTP the PHP configuration is still invalid, we need to set the sendmail_path correctly. That’s actually super easy, just create a file containing the following lines:

Save it as /path/to/php-mail.conf to mount it into the container later on.

### Putting it all together

To run it, you would need to mount the following things:

• /path/to/php-mail.conf to /usr/local/etc/php/conf.d/mail.ini
• /path/to/ssmtp.conf to /etc/ssmtp/ssmtp.conf

Thus a Docker Compose configuration may look like:

Give it a try and let me know if that doesn’t work!

Docker resources:

Some sSMTP resources that helped me configuring things: