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.
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
WARNING: [pool www] child 7 said into stderr: "sh: 1: -t: not found"
Hard times to start debugging that issue..
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:
Setup for the sSMTP
Configuring the sSMTP is easy.
Basically, all you need to do is to specify the address to the mail hub using the
However, as my mail server is running on a different physical server I also want to enable encryption, so I set
Docker containers usually get cryptic names, so I reset the hostname using the
And last but not least I allowed the applications to overwrite of the From field in emails using the
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
Even if we installed the sSMTP the PHP configuration is still invalid, we need to set the
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:
- your PHP scripts to wherever your sources are expected..
Thus a Docker Compose configuration may look like:
Give it a try and let me know if that doesn’t work!
- Docker Compose
- My binfalse/php-fpm-extended extended (entailed for my needs, e.g. also includes MySQL)
- My Dockerfile in a Git repository at GitHub
- Dockerfile reference
Some sSMTP resources that helped me configuring things:
- network (54) ,
- security (24) ,
- software (136) ,
- web (69) ,
- website (10) ,
- administration (22) ,
- php (10) ,
- howto (14) ,
- snippet (4)
- docker (8) ,
- mail (6) ,
- php (2) ,
- config (5) ,
- fix (7) ,
- log (4) ,
- network (64) ,
- snippet (10) ,
- ssl (5)