I created this image because the official PHP repository on Docker Hub doesn't have any MTA (Mail Transfer Agent) installed, meaning that the mail()
PHP function won't work and cause this peculiar error:
WARNING: [pool www] child 6 said into stderr: "sh: 1: -t: not found"
This repository adds a simple SMTP client, msmtp, so that you can transmit mail to an SMTP server (for example at a free mail provider, like Gmail) which takes care of further delivery.
Read more about msmtp here: http://msmtp.sourceforge.net/.
This example is similar to how you'd run it in a production setting, except that for testing purposes we call the send.php
file directly via docker exec
and not through a webserver like nginx. It uses Docker swarm mode (17.06+), so make sure you run docker swarm init
before you begin.
- Clone the GitHub repo:
git clone https://github.com/ilyasotkov/docker-php-msmtp.git
, thencd docker-php-msmtp/example
. - Open the
msmtprc
file and replaceexample@gmail.com
with an address of your Gmail account. Testing mail will be sent from this address. - Add the Gmail account's password to
gmail_password
file. A Docker secret will be created from that file. - Modify
send.php
and replaceyour-mailbox@example.com
with an email address that you indend receiving email to. - Run
docker stack deploy -c docker-stack.yaml mailer
. You PHP service is now running. Rundocker ps
to check the container is up and grab its ID for the next step. - You can now test sending email by running
docker exec <container id> php /send.php
. Check your mailbox now!